What should be paid attention to when using RecycleView

Keywords: Android Java

Although RecycleView is very powerful, such as efficient, such as highly decoupled, such as generic restrictions, such as efficient refreshing and animation effects when adding or deleting item s, it is not so stupid when it is powerful. Here's a summary of the points that need attention.

1. First, the RecycleView does not define the onItemClick and LongOnItemClick events. The user needs to define the callback listener interface himself.

In the callback process, you should also pay attention to some details, similar to the problem that the countdown displayed during the quick slide of listview will be displayed in disorder, which may appear: clearly click the first item, but the callback monitor prompts that you click the second or other items.

Solutions to such problems: in fact, the reason for the problem is that the position s of the operating item s are not unified. The solution is to deal with this inconsistency by constructing methods

import java.util.List;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyViewHolder> {

    private List<String> list;
    private OnItemClickListener mOnItemClickListener;

    public MyRecyclerAdapter(List<String> list) {
        // TODO Auto-generated constructor stub
        this.list = list;
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView tv;

        public MyViewHolder(View view) {
            super(view);
            tv = (TextView)view.findViewById(android.R.id.text1);

        }

    }

    @Override
    public int getItemCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        //Binding data
        holder.tv.setText(list.get(position));
        //Making non empty judgments to enhance code robustness
        if(mOnItemClickListener!=null){
            holder.itemView.setOnClickListener(new OnClickListener() {
/**
  *The point to be noticed is here, in the position and onClick methods of the onBindViewHolder method
  *Problem caused by inconsistent position in callback method of.
  *Solution: you can encapsulate this onClick click event in an internal class. When you click, you can directly
  *The position in the onBindViewHolder method is passed in through the construction, which is avoided until you click
  *The position is passed in only after the onClick method is executed, which avoids the disorder of the position.
  */
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(v, position);
                }
            });
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int arg1) {
        // Create ViewHolder
        MyViewHolder holder = new MyViewHolder(View.inflate(viewGroup.getContext(), android.R.layout.simple_list_item_1, null));
        return holder;
    }

    public void addData(int position){
        list.add(position,"additem"+position);
        //Prompt refresh - affects efficiency
//      notifyDataSetChanged();
        notifyItemInserted(position);
    }
    public void removeData(int position){
        list.remove(position);
        notifyItemRemoved(position);
    }

    public interface OnItemClickListener{
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener){
        this.mOnItemClickListener = listener;
    }


}

2,

Posted by wgordonw1 on Sun, 05 Jan 2020 19:50:54 -0800