ListView and RecyclerView remember the summary by default

Use the ListView or RecyclerView project to remember the default number of items or selected background needs to change their own background, this time query some information on the Internet, said ListView is very good implementation, Because it has a setSelection(position) method by default, but this effect and I want some differences, combined with some materials on the Internet, as well as some of my own practice, to see how to complete it, some said that the RecyclerView implementation is a little difficult. But my implementation is the same as ListView. Let’s look at # # # # # # update CSDN http://blog.csdn.net/wuyinlei/article/details/52576521 ListView

  • First, I define an int variable inside the Adapter to record which item is selected (and which item is selected by default).
Private int mPosition = 3; public voidsetPosition(int position) {
        mPosition = position;
    }

Copy the code
  • GetView (int I, View View, ViewGroup ViewGroup) : getView(int I, View View, ViewGroup ViewGroup) : getView(int I, View View, ViewGroup ViewGroup) : getView(int I, View View, ViewGroup ViewGroup)
  if (mPosition == i) {
            holder.mReRe.setBackground(mContext.getResources().getDrawable(R.drawable.spinner_ab_focused_example));
        } else {
            holder.mReRe.setBackground(mContext.getResources().getDrawable(R.drawable.spinner_ab_disabled_example));
        }
Copy the code
  • Finally, use it in an Activity or fragment as follows
   mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<? > adapterView, View view, int i, long l) { mAdapter.setPosition(i); / / passed on the location of the current click mAdapter. NotifyDataSetChanged (); // Tell listView to refresh UI}});Copy the code

RecyclerView implementation and Listview implementation steps are the same, but RecyclerView click events need to write their own.

  • First set the int variable record in the Adapter
Private int mPosition = 0; public voidsetPosition(int position) {
        mPosition = position;
    }
Copy the code
  • Then use onBindViewHolder(ViewHolder holder, int Position) to determine whether the positions of the items to be displayed are equal. If they are equal, set the desired background. If they are not equal, that is the default background (set by yourself).
 if(mPosition == position){ holder.mReRe.setBackground(holder.itemView.getContext().getResources().getDrawable(R.drawable.spinner_ab_focused_example )); }else{ holder.mReRe.setBackground(holder.itemView.getContext().getResources().getDrawable(R.drawable.spinner_ab_disabled_exampl e)); }Copy the code
  • Set click events (inside adapter)
  private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position, String s);
    }
Copy the code
  • Finally, use it in an Activity or fragment as follows
 mAdapter.setOnItemClickListener(new RecyclerViewAdapter.OnItemClickListener() { @Override public void onItemClick(View view, int position, String s) { mAdapter.setPosition(position); / / pass the current click position mAdapter. NotifyDataSetChanged (); // Notification refresh}});Copy the code

Finally look at the unified effect: