如何为 RecyclerView 设置 onItemClickListener? [复制]

Posted

技术标签:

【中文标题】如何为 RecyclerView 设置 onItemClickListener? [复制]【英文标题】:How to set onItemClickListener for RecyclerView? [duplicate] 【发布时间】:2017-05-03 05:21:15 【问题描述】:

我正在尝试实现 recyclerView,但如何为我的 recyclerView 设置 onItemClickListener?

已经有一些关于 SO 的答案,但它们比我获取 RecyclerView 的 onItemClickListener 所需的要简短。

获取 RecyclerView 的 onItemClickListener 的最简单方法是什么?

【问题讨论】:

很好的解释***.com/a/31199564/5788247 【参考方案1】:

android 中,RecyclerView 没有像 ListView 中的任何listeners。所以你需要通过扩展RecyclerView.OnItemTouchListener来创建你自己的类。

创建一个类RecyclerTouchListener.java

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener 

  private GestureDetector gestureDetector;
  private ClickListener clickListener;

public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) 
    this.clickListener = clickListener;
    gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() 
        @Override
        public boolean onSingleTapUp(MotionEvent e) 

            return true;

        

        @Override
        public void onLongPress(MotionEvent e) 

            View child = recyclerView.findChildViewUnder(e.getX(), e.getY());

            if (child != null && clickListener != null) 

                clickListener.onLongClick(child, recyclerView.getChildAdapterPosition(child));

            
        
    );


@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) 

    View child = rv.findChildViewUnder(e.getX(), e.getY());

    if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) 

        clickListener.onClick(child, rv.getChildAdapterPosition(child));

    
    return false;


@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) 


@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) 



public interface ClickListener 

    void onClick(View view, int position);

    void onLongClick(View view, int position);
   
 

然后在activity中,重写onClick方法。

 recyclerView.addOnItemTouchListener(new RecyclerTouchListener(context, recyclerView, new RecyclerTouchListener.ClickListener() 
        @Override
        public void onClick(View view, int position) 

         //   Toast.makeText(getActivity(), position+ " is selected successfully", Toast.LENGTH_SHORT).show();

                //handle click event

        

        @Override
        public void onLongClick(View view, int position) 

        
    ));

这样就可以处理recyclerviewitem click event了。

【讨论】:

...@Vinoth Vino 如果我的回答对您有帮助,请接受。快乐编码... :) @SabirHossain @VinothVino 在 RecyclerTouchListener 类中,getChildPosition 已弃用。【参考方案2】:

使用此代码定义一个名为 RecyclerItemClickListener 的类

class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener 
    private OnItemClickListener mListener;

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

    private GestureDetector mGestureDetector;

    RecyclerItemClickListener(Context context, OnItemClickListener listener) 
        mListener = listener;
        mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() 
            @Override
            public boolean onSingleTapUp(MotionEvent e) 
                return true;
            
        );
    

    @Override
    public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) 
        View childView = view.findChildViewUnder(e.getX(), e.getY());
        if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) 
            mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
        
        return false;
    

    @Override
    public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) 
    

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) 
    

然后在您的活动中使用此代码

recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(MainActivity.this, new RecyclerItemClickListener.OnItemClickListener() 
    @Override
    public void onItemClick(View view, int position) 
    
));

【讨论】:

以上是关于如何为 RecyclerView 设置 onItemClickListener? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

如何为 RecyclerView 列表元素设置单击侦听器以显示 AlertDialog?

如何为 recyclerView 上的两个不同事件设置监听器?

如何为android recyclerview中的最大字符串设置textview高度?

如何为最后一个元素为 RecyclerView 添加边距?

如何为无限滚动的 RecyclerView 绘制背景

如何为从 Firestore 填充的 RecyclerView 实现过滤器?