Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何根据recyclerview Android的位置动态更改布局中的元素?_Android_Android Recyclerview - Fatal编程技术网

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

如何根据recyclerview Android的位置动态更改布局中的元素?,android,android-recyclerview,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.userna

我的数据集有一个元素
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
消失了
userId
,但其中一些
userId
与前一职位的
userId
不相同的
username
也消失了

为了清楚起见,我附加了一些图片来清楚地显示我想要的东西

这就是我想要的:

final Item item = itemsArray.get(position);

if(position - 1 > -1){
   if(items.get(position-1).getUserId() == item.getUserId()) {
      holder.username.setVisibility(View.GONE);
   }
}

但最后,它变成了下图,正如您看到的
具有不同的
用户ID
用户名
也消失了

所以,我的问题是,我如何检查前一个项目的
userId
是否与当前位置的
userId
相同,这样它就不会出现在我附加的第二个图像中

如果有其他解决方案,请让我知道。提前感谢。

使用此代码

 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”的某些视图。

在适配器中扫描holder并在以后更改它是错误的。
在适配器中设置数据之前,应准备好数据。更改数据的模型(例如,将字段
boolean isShowName
添加到
Item
类)

您的用户ID是字符串类型吗?是
int
类型b尝试为“holder.username”添加else条件逻辑是否有效..您可以更新答案吗?因为您没有在答案中添加else逻辑此代码也可以工作。检查一下。如果我的答案是正确的,请接受我的附件。