如何根据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逻辑此代码也可以工作。检查一下。如果我的答案是正确的,请接受我的附件。