Android RecyclerView更改所有项目图像视图

时间:2017-06-30 17:53:37

标签: android listview android-recyclerview

我有一些带有一些物品的RecyclerView。
我想更改项目上的所有ImageView图像(无滚动)
我在onBindViewHolder中的部分代码是:

int count = parent.getChildCount();
v = parent.getChildAt(position);
while(i<count)
   if (i != position) {
      v = parent.getChildAt(i);
      ImageView imageView1 = (ImageView) v.findViewById(R.id.imgPlaySound);
      imageView1.setImageResource(R.mipmap.playsound);
      LinearLayout linearSeek1 = (LinearLayout) v.findViewById(R.id.linearSeek);
      linearSeek1.setVisibility(View.GONE);
   }
   i++;
}

这部分代码工作正常,但如果我将一些项目添加到我的RecyclerView并滚动我的项目,我的代码将无法访问我的所有项目
我的自定义RecyclerView类是活动代码的单独文件

2 个答案:

答案 0 :(得分:0)

也许这有帮助..

ViewGroup parent = (ViewGroup) holder.itemView.getParent();
for (int i = 0; i < parent.getChildCount(); i++) {
    View child = parent.getChildAt(i);
    //your code here..
}

答案 1 :(得分:-1)

将onBindViewHolder中的每个视图添加到ArrayList,然后遍历此ArrayList并执行操作。

    ArrayList<ViewHolder> views = new ArrayList<>();//call in the constructor make it a class variable so it can accessed globally;

然后在你的“onBindViewHolder”上执行此操作

   @Override
public void onBindViewHolder(final ViewHolder holder, final int position) {

  views.add(holder)
}

现在你可以遍历arraylist并设置你的观点

int count = views.size();
for(int i=0; i<count; i++)
{
  YourViewHolder v = views.get(i); 
  //call imageview from the viewholder object by the variable name used to instatiate it
  ImageView imageView1 = v.imageViewFromHolder;
  imageView1.setImageResource(R.mipmap.playsound);
  LinearLayout linearSeek1 = v.linearLayoutFromHolder 
  linearSeek1.setVisibility(View.GONE);
}