RecycleView点击事件的处理

recycleView中是没有对点击事件的处理的,所以需要我们自己设置,也就是本章要提到的使用接口回调来实现recycleView的item的点击事件的处理。
先上效果图


点击效果.gif

长按效果.gif

ok,接下来说一下如何设置点击事件。

public class RecycleViewAdapter extends 
          RecyclerView.Adapter {

       private List list;
       private OnitemClick onitemClick;   //定义点击事件接口
       private OnLongClick onLongClick;  //定义长按事件接口
       public RecycleViewAdapter (List list) {
              this.list = list;
       }
       //定义设置点击事件监听的方法
        public void setOnitemClickLintener (OnitemClick onitemClick) {
               this.onitemClick = onitemClick;
         }
        //定义设置长按事件监听的方法
         public void setOnLongClickListener (OnLongClick onLongClick) {
               this.onLongClick = onLongClick;
          }

         //定义一个点击事件的接口
         public interface OnitemClick {
               void onItemClick(int position);
          }
         //定义一个长按事件的接口
         public interface OnLongClick {
              void onLongClick(int position);
          }
        @Override
         public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
               View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_view_item,null);
               ViewHolder viewHolder = new ViewHolder(view);
               return viewHolder;
         }

        @Override
         public void onBindViewHolder(ViewHolder holder, final int position) {
                holder.textView.setText(list.get(position));
                if (onitemClick != null) {
                     holder.textView.setOnClickListener(new View.OnClickListener() {
                         @Override
                         public void onClick(View v) {
                              //在TextView的地方进行监听点击事件,并且实现接口
                              onitemClick.onItemClick(position);
                         }
                    });
                }

               if (onLongClick != null) {
                    holder.textView.setOnLongClickListener(new View.OnLongClickListener() {
                     @Override
                     public boolean onLongClick(View v) {
                           //在TextView的地方进行长按事件的监听,并实现长按接口
                            onLongClick.onLongClick(position);
                            return true;
                      }
                 });
             }
      }


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

       class ViewHolder extends RecyclerView.ViewHolder {

             private TextView textView;
             public ViewHolder(View itemView) {
                 super(itemView);
                 textView = itemView.findViewById(R.id.item);
           }
        }
     }

接下来在activity中使用就可以了,adapter中实现点击和长按回调的接口:

adapter.setOnitemClickLintener(new RecycleViewAdapter.OnitemClick() {
            @Override
            public void onItemClick(int position) {
                Toast.makeText(MainActivity.this,"点击了一下"+position,Toast.LENGTH_SHORT).show();
            }
        });

 adapter.setOnLongClickListener(new RecycleViewAdapter.OnLongClick() {
            @Override
            public void onLongClick(int position) {
                Toast.makeText(MainActivity.this,"长按了一下"+position,Toast.LENGTH_SHORT).show();
            }
       });

你可能感兴趣的:(RecycleView点击事件的处理)