如何获取所有RecyclerView ViewHolder?

时间:2016-04-14 12:28:37

标签: android android-recyclerview android-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" />

4 个答案:

答案 0 :(得分:2)

在适配器类中存储一个标志,如var url = "exampleUrl?"+JSON.stringify(my_array)+"&api_key="+api+"&format=json"; 。在onclicklistener中将其设置为private boolean clicked = false;并调用true

notifiyDatasetChanged()方法中添加if if this this

onBindViewHolder

答案 1 :(得分:2)

没有经过测试!!!

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

答案 2 :(得分:2)

如果您想要为所有可见视图设置动画,那么

notifyDatasetChanged()及其兄弟方法并不起作用。 要设置动画,您可以更新数据源,但不能调用notifyDatasetChanged(),而是获取所有ViewHolders并自行设置动画。在滚动时,任何重新绑定的项目都将具有正确的状态,您可以通过编写与 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:未经测试的代码,但它应该有效。

答案 3 :(得分:0)

您可以创建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捕获异常