簡體   English   中英

Android RecyclerView 更改所有項目圖像視圖

[英]Android RecyclerView change all item imageviews

我有一個包含一些項目的 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 類是活動代碼的單獨文件

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

也許這有幫助..

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM