android - 如何根据 recyclerview Android 的位置动态更改布局中的元素?

标签 android android-recyclerview

我的数据集有一个元素 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 的项目他们的 usernamegone,但其中一些 userId 与之前位置的 userId 不相同 username消失了

为了清楚起见,我附上一些图片以清楚地显示我想要的内容。

这就是我想要的:

enter image description here

但最终,它变成了下图,因为您看到 item 具有不同的 userIdusername 也“消失了”。

enter image description here

所以,我的问题是,如何检查上一个项目的 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/

相关文章:

android int到十六进制转换

android - 无法使用 Realm 实时更新recyclerView

android - 为什么 TextView 中的文本在回收器 View 中被截断?

java - 如何制作像 WhatsApp 聊天 Activity 一样的 RecyclerView?

android - Epg 的 2 维滚动图表或 android 中的某些时间表

android - 我如何在android中为按钮实现非矩形形状

Android/Koin - 如何告诉 koin 应该重新初始化单个依赖项

android - 找不到参数的方法 release()

android - android中的内部存储

android - RecyclerView.OnClickListener 与 View.OnClickListener