【发布时间】:2021-10-14 02:12:09
【问题描述】:
尝试隐藏具有空属性的卡片时,recyclerView 中的空格
嗨,我是新来的。所以这是我的第一个问题。我正在尝试将 google Books API 用于一项任务,当我实现所有内容时,我看到许多没有图片、标题或作者的书籍, 出于显而易见的原因,我不希望它们出现在应用程序的搜索中。我试图将属性card.visibility = View.GONE(在try-catch块内)应用于recyclerview中的那些cardview,但是这样做时它们会在recycler列表中留下空白,我不希望它们出现。 你怎么能解决这个问题?我附上部分代码和图像,以便您可以看到我这样做时剩余的空间
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"
}
}
}
这就是应用在没有“visibility = 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 之间有很多空白空间,我该如何消除? (英语不是我的第一个 lenguaje,很抱歉,如果这很难阅读,我表示歉意)
【问题讨论】:
-
`tarjeta` 是指父布局吗?
-
catch (e: NullPointerException)hahahaha ...你为什么不先检查一些东西是否为空? -
我认为在这种情况下父布局是recyclerView,而“tarjeta”是recyclerView中的每个cardView
标签: java android android-studio kotlin visibility