RecyclerView的点击事件

本文介绍了一种为Android的RecyclerView实现点击及长按事件监听的方法。通过自定义RecyclerItemClickListener类并实现RecyclerView.OnItemTouchListener接口,可以有效捕捉用户点击行为。文章提供了实现细节与使用示例。
摘要由CSDN通过智能技术生成
用到RecyclerView的点击事件查的,记录一下,来源于 android v7兼容包RecyclerView的使用(四)——点击事件的不同方式处理
 
 
package com.mama.kanzhihudaily.ClickListener;

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

/**
 * Created by dev on 10/31/15.
 */
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {

    private OnItemClickListener mListener;
    private GestureDetector mGestureDetector;


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

         void onItemLongClick(View view, int position);
    }
    public RecyclerItemClickListener(Context context,final RecyclerView recyclerView, OnItemClickListener listener) {

        mListener = listener;

        mGestureDetector = new GestureDetector(context, new GestureDetector.OnGestureListener() {
            @Override
            public boolean onDown(MotionEvent motionEvent) {
                return false;
            }

            @Override
            public void onShowPress(MotionEvent motionEvent) {

            }

            @Override
            public boolean onSingleTapUp(MotionEvent motionEvent) {
                return true;
            }

            @Override
            public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
                return false;
            }

            @Override
            public void onLongPress(MotionEvent motionEvent) {

                View view = recyclerView.findChildViewUnder(motionEvent.getX(),motionEvent.getY());
                if (view != null && mListener != null){
                    mListener.onItemLongClick(view,recyclerView.getChildAdapterPosition(view));
                }
            }

            @Override
            public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
                return false;
            }
        });
    }

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

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

    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }
}
</pre><pre name="code" class="java">使用:
        <pre name="code" class="java">recyclerView<span style="font-family: Arial, Helvetica, sans-serif;">.addOnItemTouchListener(new RecyclerItemClickListener(this,gridView,listener));</span>
<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="java">    RecyclerItemClickListener.OnItemClickListener listener = new RecyclerItemClickListener.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            Log.d("tony onItemClick",position+"");
        }

        @Override
        public void onItemLongClick(View view, int position) {
            Log.d("tony onItemLongClick",position+"");
        }
    };


 

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值