【问题标题】:Android RecyclerView change all item imageviewsAndroid RecyclerView 更改所有项目 imageviews
【发布时间】:2017-06-30 17:53:37
【问题描述】:

我有一个包含一些项目的 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 类是活动代码的单独文件

【问题讨论】:

标签: android listview android-recyclerview


【解决方案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)
}

现在您可以遍历数组列表并设置视图

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);
}

【讨论】:

    【解决方案2】:

    也许这有帮助..

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-10
      • 2018-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多