我的数据集有一个元素 userId
。我想检查位置的 userId
是否等于上一个位置的 userId
,然后将 TextView
设置为 GONE
.
到目前为止,我在 onBindViewHolder
中尝试了什么:
final Item item = itemsArray.get(position);
if(position - 1 > -1){
if(items.get(position-1).getUserId() == item.getUserId()) {
holder.username.setVisibility(View.GONE);
}
}
但最后,一些具有相同 userId
的项目他们的 username
是 gone
,但其中一些 userId
与之前位置的 userId
不相同 username
也消失了
。
为了清楚起见,我附上一些图片以清楚地显示我想要的内容。
这就是我想要的:
但最终,它变成了下图,因为您看到 item
具有不同的 userId
,username
也“消失了”。
所以,我的问题是,如何检查上一个项目的 userId
是否与当前位置的 userId
相同,所以它不会结束到第二张图片 I附上。
如果有其他解决方案,请告诉我。提前致谢。
最佳答案
使用此代码
final Item item = itemsArray.get(position);
holder.username.setVisibility(View.VISIBLE);
if(position - 1 > -1){
if(items.get(position-1).getUserId() == item.getUserId()) {
holder.username.setVisibility(View.GONE);
}else{
holder.username.setVisibility(View.VISIBLE);
}
}
当您使用 RecyclerView 时,holder.username 状态为“GONE”的某些 View 将被重用。
关于android - 如何根据 recyclerview Android 的位置动态更改布局中的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46377983/