其他分享
首页 > 其他分享> > Android开发---RecyclerView子项点击事件与SwipeRecyclerView外部框架结合

Android开发---RecyclerView子项点击事件与SwipeRecyclerView外部框架结合

作者:互联网

1.子项点击事件概述

         recyclerView作为列表展示,每个item能够做点击事件,比如点开文件夹之类的,在这里主要写两种方法进行item的点击事件,第一种方式是利用监听器事项,第二种方式是利用已经写好的外部框架SwipeRecyclerView

2.监听器实现

         在adapter类中可以定义监听器进行实现,在activity中实现点击事件的具体逻辑

Step1:定义监听器和接口

//设置监听器
    private OnRecyclerItemClickListener mOnItemClickListener;
    public void setRecyclerItemClickListener(OnRecyclerItemClickListener listener){
        mOnItemClickListener=listener;

    }
    public interface OnRecyclerItemClickListener{
        void onRecyclerItemClick(int position);
    }

Step2:在MyViewHolder中设置item点击事件:

 public class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView tv;
        public MyViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            tv=itemView.findViewById(R.id.tv1);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(mOnItemClickListener==null){
                        mOnItemClickListener.onRecyclerItemClick(getAdapterPosition());
                    }
                }
            });
        }
    }

Step3:在activity/fragment中实现点击事件的具体逻辑:可以做跳转、输出等等

  myrevAdpter.setRecyclerItemClickListener(new MyrevAdpter.OnRecyclerItemClickListener() {
            @Override
            public void onRecyclerItemClick(int position) {
                Log.e("tag1",String.valueOf(position));
            }
        });

        利用监听器实现就是这三步就可以啦

3.利用SwipeRecyclerView外部框架实现

     Step1:首先进行导包:在build中添加以下代码

 implementation 'com.yanzhenjie.recyclerview:x:1.3.2'

Step2:在xml文件中写这里的SwipeRecyclerView

 <com.yanzhenjie.recyclerview.SwipeRecyclerView
            android:id="@+id/rv_resourcemanage"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </com.yanzhenjie.recyclerview.SwipeRecyclerView>

Step3:声明SwipeRecyclerView并找到控件

 private SwipeRecyclerView recyclerView;
 recyclerView =findViewById(R.id.rv_resourcemanage);

Step4:与RecyclerView一致进行设置(这里需要注意的是setAdapter必须在所有关于adapter操作之后执行)

 LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MyResourceActivity.this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        resourceAdapter = new ResourceAdapter(fileList, MyResourceActivity.this,map_check);
        recyclerView.setAdapter(resourceAdapter);

Step5:直接调用SwipeRecyclerView中的方法就可以进行子项的点击事件

 //可以直接调用swipeRecyclerView框架里面的点击子项、点击长按事件
        recyclerView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
               //做点击事件的逻辑
                }
            }
        });
  //长按
        recyclerView.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public void onItemLongClick(View view, int position) {
             //做长按点击事件的逻辑
                    }
                });

       利用SwipeRecyclerView进行子项的点击和长按点击就是这几步啦

标签:void,子项,---,点击,监听器,Android,recyclerView,public,SwipeRecyclerView
来源: https://blog.csdn.net/m0_57098080/article/details/121643996