Android RecyclerView单击、长按事件:基于OnItemTouchListener +GestureDetector标准实现(二),封装抽取成通用工具类



Android RecyclerView单击、长按事件:基于OnItemTouchListener +GestureDetector标准实现(二),封装抽取成通用工具类


我写的附录文章2,介绍了Android如何基于OnItemTouchListener +GestureDetector实现单击、长按事件的监听,由于如今RecyclerView在Android开发是如此的普遍,以及RecyclerView的单击事件是如此的常用,如果像附录文章2那样把一堆事件监听写到业务逻辑代码里面,那得写多少遍重复代码!所以我把给RecyclerView添加点击事件的Listener抽取、封装成一个常用的工具栏类RecyclerViewUtil。
在给一个RecyclerView添加单击事件时候,跟ListView的监听事件一样,只需要简单的写如下代码即可完成:

//第一步,先初始化RecyclerViewUtil,传入当前的RecyclerView对象和上下文指针this:Context
        RecyclerViewUtil util=new RecyclerViewUtil(this,mRecyclerView);

        //第二步,直接set
        util.setOnItemClickListener(new RecyclerViewUtil.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View view) {
                //Toast.makeText(getApplicationContext(),position+" 单击",Toast.LENGTH_SHORT).show();
            }
        });


长按事件也类似:

//先初始化RecyclerViewUtil,传入当前的RecyclerView对象和上下文指针this:Context
        RecyclerViewUtil util=new RecyclerViewUtil(this,mRecyclerView);

        util.setOnItemLongClickListener(new RecyclerViewUtil.OnItemLongClickListener() {
            @Override
            public void onItemLongClick(int position, View view) {
                //Toast.makeText(getApplicationContext(),position+" 长按",Toast.LENGTH_SHORT).show();
            }
        });


封装、抽取好的通用RecyclerViewUtil.java全部代码:

package zhangphil.app;

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


/**
 * Created by Phil on 2016/11/19.
 */

public class RecyclerViewUtil {
    private RecyclerView mRecyclerView = null;
    private GestureDetector mGestureDetector = null;
    private RecyclerView.SimpleOnItemTouchListener mSimpleOnItemTouchListener;
    private OnItemClickListener mOnItemClickListener = null;
    private OnItemLongClickListener mOnItemLongClickListener = null;
    private Context context;

    public RecyclerViewUtil(Context context, RecyclerView recyclerView) {
        this.context = context;
        this.mRecyclerView = recyclerView;

        mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            //长按事件
            @Override
            public void onLongPress(MotionEvent e) {
                super.onLongPress(e);
                if (mOnItemLongClickListener != null) {
                    View childView = mRecyclerView.findChildViewUnder(e.getX(), e.getY());
                    if (childView != null) {
                        int position = mRecyclerView.getChildLayoutPosition(childView);
                        mOnItemLongClickListener.onItemLongClick(position, childView);
                    }
                }
            }

            //单击事件
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                if (mOnItemClickListener != null) {
                    View childView = mRecyclerView.findChildViewUnder(e.getX(), e.getY());
                    if (childView != null) {
                        int position = mRecyclerView.getChildLayoutPosition(childView);
                        mOnItemClickListener.onItemClick(position, childView);
                        return true;
                    }
                }

                return super.onSingleTapUp(e);
            }
        });

        mSimpleOnItemTouchListener = new RecyclerView.SimpleOnItemTouchListener() {
            @Override
            public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
                if (mGestureDetector.onTouchEvent(e)) {
                    return true;
                }
                return false;
            }
        };

        mRecyclerView.addOnItemTouchListener(mSimpleOnItemTouchListener);
    }

    public void setOnItemClickListener(OnItemClickListener l) {
        mOnItemClickListener = l;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener l) {
        mOnItemLongClickListener = l;
    }

    //长按事件接口
    public interface OnItemLongClickListener {
        public void onItemLongClick(int position, View view);
    }

    //单击事件接口
    public interface OnItemClickListener {
        public void onItemClick(int position, View view);
    }
}


使用时候直接把RecyclerViewUtil.java复制到自己的项目代码工程中即可。


附录:
1,《RecyclerView点击事件》链接:http://blog.csdn.net/zhangphil/article/details/46771865
2,《Android RecyclerView单击、长按事件标准实现:基于OnItemTouchListener + GestureDetector》:http://blog.csdn.net/zhangphil/article/details/53230157

posted on 2016-11-19 16:20  疯子123  阅读(290)  评论(0编辑  收藏  举报

导航