尝试隐藏具有空属性的卡片时,如何消除 recyclerView 中的空白?
How can i eliminate Empty spaces in recyclerView when trying to hide cards with null properties?
尝试隐藏具有空属性的卡片时,recyclerView 中的 space 为空
嗨,我是新来的。所以这是我的第一个问题。我正在尝试使用 google 书籍 API 来完成一项任务,当我实施所有内容时,我看到许多书籍没有图片、标题或作者,
出于显而易见的原因,我不希望它们出现在应用程序的搜索中。我试图将 属性 card.visibility = View.GONE(在 try-catch 块内)应用于 recylcerview 内的那些 cardview,但是在这样做时它们会留空 space在回收站列表中,我不希望它们出现。
你怎么解决这个问题?我附上了部分代码和图像,这样你就可以看到我做 tath
时剩下的 spaces
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"
}
}
}
这是应用在没有“可见性 = View.GONE”属性的情况下的工作方式
This is how the app works without the "GONE" atribute
这是激活了“visibility = View.GONE”属性
And this is with "visibility = View.GONE" attribute activated
如您所见,cardViews 之间有很多空的 spaces,我该如何消除它?
(英语不是我的第一个语言,很抱歉,如果这难以阅读,我表示歉意)
您需要在 onBindViewHolder 之前从 libros.items 中删除空数据。因为项目将在您尝试捕获时被漠不关心地支付
有很多方法可以做你想做的事(折叠列表中的项目),但老实说,在你尝试显示它之前,从你的列表中过滤掉无效数据要容易得多。
类似于:
// 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)
这样就不会弄乱您需要忽略的项目,您不需要检查内容是否有效 - 在将它传递给适配器之前您已经检查过了!所有数据都很好,现在只是一个普通的项目列表
尝试隐藏具有空属性的卡片时,recyclerView 中的 space 为空
嗨,我是新来的。所以这是我的第一个问题。我正在尝试使用 google 书籍 API 来完成一项任务,当我实施所有内容时,我看到许多书籍没有图片、标题或作者, 出于显而易见的原因,我不希望它们出现在应用程序的搜索中。我试图将 属性 card.visibility = View.GONE(在 try-catch 块内)应用于 recylcerview 内的那些 cardview,但是在这样做时它们会留空 space在回收站列表中,我不希望它们出现。 你怎么解决这个问题?我附上了部分代码和图像,这样你就可以看到我做 tath
时剩下的 spaces 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"
}
}
}
这是应用在没有“可见性 = View.GONE”属性的情况下的工作方式
This is how the app works without the "GONE" atribute
这是激活了“visibility = View.GONE”属性
And this is with "visibility = View.GONE" attribute activated
如您所见,cardViews 之间有很多空的 spaces,我该如何消除它? (英语不是我的第一个语言,很抱歉,如果这难以阅读,我表示歉意)
您需要在 onBindViewHolder 之前从 libros.items 中删除空数据。因为项目将在您尝试捕获时被漠不关心地支付
有很多方法可以做你想做的事(折叠列表中的项目),但老实说,在你尝试显示它之前,从你的列表中过滤掉无效数据要容易得多。
类似于:
// 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)
这样就不会弄乱您需要忽略的项目,您不需要检查内容是否有效 - 在将它传递给适配器之前您已经检查过了!所有数据都很好,现在只是一个普通的项目列表