如何获取所有的RecyclerView ViewHolder?

How to get all RecyclerView ViewHolder?

我想在单击其中一项时更改所有项目的文本颜色。我的问题是颜色仅在可见项目上发生变化。隐形物品的颜色根本不会改变。

新: 另外我想知道如何只改变点击项目的颜色。

EffectPanelRecyclerViewAdapter.java:

public class EffectPanelRecyclerViewAdapter extends RecyclerView.Adapter<EffectPanelRecyclerViewAdapter.ViewHolder> {
private ArrayList<String> mDataset = null;
private MyItemClickListener mItemClickListener;

public EffectPanelRecyclerViewAdapter(ArrayList<String> datats) {
    mDataset = datats;
}


public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    public TextView mTextView;
    private MyItemClickListener mListener;

    public ViewHolder(View v, MyItemClickListener listener) {
        super(v);
        mListener = listener;
        mTextView = (TextView) v.findViewById(R.id.tv);
        v.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (mListener != null) {
            Log.d("hello", "getAdapterPosition=" + getAdapterPosition());
            mListener.onItemClick(v, getAdapterPosition());
        }
    }

}

@Override
public int getItemCount() {
    return mDataset.size();
}

public interface MyItemClickListener {
    public void onItemClick(View view, int postion);
}

/**
 * ViewHolder
 */
@Override
public EffectPanelRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // create a new view
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
    // set the view's size, margins, paddings and layout parameters
    ViewHolder vh = new ViewHolder(v, mItemClickListener);
    Log.d("hello", "onCreateViewHolder");
    return vh;
}

@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
    Log.d("hello", "onBindViewHolder,position=" + position);
    ViewHolder mHolder = (ViewHolder) holder;
    mHolder.mTextView.setText(mDataset.get(position));
    mHolder.mTextView.setTag(position);
    mHolder.mTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mItemClickListener.onItemClick(v, position);
        }
    });
}

public void setOnItemClickListener(MyItemClickListener listener) {
    this.mItemClickListener = listener;
}

}

MainActivity.java:

public class MainActivity extends AppCompatActivity {

private ArrayList<String> mDatas;
private RecyclerView mRecyclerView;
private EffectPanelRecyclerViewAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initDatas();
    mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview_horizontal);
    final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
    mRecyclerView.setLayoutManager(linearLayoutManager);
    mAdapter = new EffectPanelRecyclerViewAdapter(mDatas);
    mRecyclerView.setAdapter(mAdapter);
    mAdapter.setOnItemClickListener(new EffectPanelRecyclerViewAdapter.MyItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            Toast.makeText(MainActivity.this, "position=" + position, Toast.LENGTH_SHORT).show();
            //I want to change all item text color.How to do it ?
            for (int i = 0; i < mAdapter.getItemCount(); i++) {
                EffectPanelRecyclerViewAdapter.ViewHolder viewHolder = (EffectPanelRecyclerViewAdapter.ViewHolder) mRecyclerView.findViewHolderForAdapterPosition(i);
                if (viewHolder != null) {
                    viewHolder.mTextView.setTextColor(Color.RED);
                    Log.d("hellotom", "if i=" + i);
                } else {
                    Log.d("hellotom", "else i=" + i);
                }
            }
        }
    });
}

private void initDatas() {
    mDatas = new ArrayList();
    for (int i = 0; i < 10; i++) {
        mDatas.add(i, "i=" + i);
    }
}

}

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
    android:id="@+id/id_recyclerview_horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerVertical="true"
    android:paddingBottom="18dp"
    android:paddingTop="15dp"
    android:scrollbars="none" />

item.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<TextView
    android:id="@+id/tv"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:drawableTop="@drawable/ic_launcher_select"
    android:textColor="@color/select_color"
    android:gravity="center"
    android:text="@string/app_name" />

像这样 private boolean clicked = false; 在您的适配器中存储一个标志 class。在 onclicklistener 中将其设置为 true 并调用 notifiyDatasetChanged()

onBindViewHolder 方法中添加一个 if 像这样

if (clicked)
   viewHolder.mTextView.setTextColor(Color.RED); 
else
   viewHolder.mTextView.setTextColor(Color.BLUE);

未测试!!!

public class EffectPanelRecyclerViewAdapter extends
    RecyclerView.Adapter<EffectPanelRecyclerViewAdapter.ViewHolder> {

    private ArrayList<DataClass> mDataset = null;

    public EffectPanelRecyclerViewAdapter(ArrayList<DataClass> datats) {
        mDataset = datats;
    }

    /**
     * ViewHolder
     */
    @Override
    public EffectPanelRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                                        int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
        // set the view's size, margins, paddings and layout parameters
        ViewHolder vh = new ViewHolder(v);
        Log.d("hello", "onCreateViewHolder");
        return vh;
    }

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

        Log.d("hello", "onBindViewHolder,position=" + position);
        holder.mTextView.setText(mDataset.get(position).text);
        holder.mTextView.setTextColor(mDataset.get(position).clicked ? Color.RED : Color.BLUE);
        holder.mTextView.setTag(position);
        holder.mTextView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mDataset.get(position).clicked = true;
                notifyItemChanged(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDataset.size();
    }


    private static class DataClass {

        public String  text;
        public boolean clicked;
    }


    private static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView mTextView;

        public ViewHolder(View v) {
            super(v);
            mTextView = (TextView) v.findViewById(R.id.tv);
        }
    }
}

notifyDatasetChanged() 如果您想为所有可见视图设置动画,则它的同级方法不起作用。 要制作动画,您可以更新数据源但不调用 notifyDatasetChanged(),而是获取所有 ViewHolder 并自己制作动画。在滚动时,任何重新绑定的项目都将具有正确的状态,您可以通过编写类似于 Ralph Bergmann 发布的代码来实现。

AnimatorSet set = new AnimatorSet();
ArrayList<Animator> animators = new ArrayList<>(mRecyclerView.getChildCount());
for (int i = 0, size = mRecyclerView.getChildCount(); i < size; i++) {
    RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(mRecyclerView.getChildAt(i));
    if (holder != null) {
        // do stuff like this
        ObjectAnimator animator = ObjectAnimator.ofFloat(holder.itemView, "alpha", 0F);
        animators.add(animator);
    }
}
set.playTogether(animators);
set.setDuration(200);
set.start();

PS: 未经测试的代码,但它应该可以工作。

您可以创建一个 ViewHolder 列表,稍后您将能够像这样更新每个 ViewHolder 中的所有文本颜色:

public class EffectPanelRecyclerViewAdapter extends RecyclerView.Adapter<EffectPanelRecyclerViewAdapter.ViewHolder> {
    private ArrayList<String> mDataset = null;

    private List<ViewHolder> viewHolderList = new ArrayList<>(); // create a list of your ViewHolder class

    private MyItemClickListener mItemClickListener;

    public EffectPanelRecyclerViewAdapter(ArrayList<String> datats) {
        mDataset = datats;
    }

    @Override
    public int getItemCount() {
        return mDataset.size();
    }

    @Override
    public EffectPanelRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
        ViewHolder vh = new ViewHolder(v, mItemClickListener);
        viewHolderList.add(vh); // add viewHolder in list
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        ViewHolder mHolder = (ViewHolder) holder;
        mHolder.mTextView.setText(mDataset.get(position));
        mHolder.mTextView.setTag(position);
        mHolder.mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                updateColors(); // now call this function to update all ViewHolders at once

            }
        });
    }

    // function to update colors
    public void updateColors() {  
        for (ViewHolder viewHolder : viewHolderList) {
            if (viewHolder != null) {
                viewHolder.mTextView.setTextColor(Color.RED);
            }
        }
    }
}

确保在设置颜色时使用 try catch 捕获异常