Skip to content
Advertisement

How can i eliminate Empty spaces in recyclerView when trying to hide cards with null properties?

Empty spaces in recyclerView when trying to hide cards with null properties

Hi, i’m new here. So this is my first question. I am trying to use the google Books API for a task and when I implement everything, I see many books without image, title or author, that for obvious reasons I do not want them to appear in the searches of the application. I tried to apply to those cardview inside the recylcerview the property card.visibility = View.GONE (inside a try-catch block), but when doing it they leave blank space inside the recycler list and I don’t want them to appear. How can you solve this? I attach part of the code and images so that you can see the spaces that remain when i do tath

   override fun onBindViewHolder(holder: LibrosHolder, position: Int) {
        holder.binding.apply {

            var autores = libros.items[position].volumeInfo.authors
            tvtitulo.text = libros.items[position].volumeInfo.title

            try {
                Glide.with(ivlibro.context)
                    .load(libros.items.get(position).volumeInfo.imageLinks.thumbnail)
                    .fitCenter()
                    .into(ivlibro)
                tvautor.text = autores[0]
                tvtitulo.text = libros.items[position].volumeInfo.title
            } catch (e: NullPointerException) {
                tarjeta.isVisible=true
                tarjeta.visibility = View.GONE
                ivlibro.setImageResource(R.mipmap.noimagendisponible)
                tvautor.text = "Sin autor"
                tvtitulo.text = "Sin título"
            }
        }
    }

This is how the app works without the “visibility = View.GONE” attribute

This is how the app works without the “GONE” atribute

And this is with “visibility = View.GONE” attribute activated

And this is with “visibility = View.GONE” attribute activated

As you can see, there is a lot of empty spaces between the cardViews how can i eliminaty that?? (english is not my first lenguaje, so sorry if this is difficult to read, i offer my appologies)

Advertisement

Answer

There are ways to do what you want (collapsing items in the list) but honestly, it’s way easier to just filter out invalid data from your list, before you try to display it.

Something like:

// get all your books from the API, however you do that
val todosLibros = getAllTheBooks()

// filter out the bad data
val buenosLibros = todosLibros
        .filterNotNull { it.autores }
        .filterNotNull { it.tvtitulo }
        .filterNotNull { it.volumeInfo?.imageLinks?.thumbnail }

// set the filtered data on your RecyclerView's adapter
// (I'm guessing you pass it in through the constructor)
recyclerView.adapter = MyCoolBooksAdapter(buenosLibros)

That way there’s no messing around with items you need to ignore, you don’t need to check stuff is valid – you already checked it before you passed it to the adapter! All the data is good, and now it’s just a normal list of items

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement