RecycleView的Item点击事件的几种实现方法

2018-05-08  本文已影响0人  留给时光吧

1.利用addOnItemTouchListener

RecycleView本身提供了addOnItemTouchListener方法,用于处理item的触摸事件,我们只需稍微加工就可以处理点击事件。这个方法需要一个OnItemTouchListener实现对象,这个接口中有以下三个方法:

public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e);
public void onTouchEvent(RecyclerView rv, MotionEvent e);
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept);

很明显都是触摸事件的回调,我们可以自己实现点击逻辑,但为了方便起见,我们可以借助SimpleOnGestureListener类帮助我们,具体如下:

public class OnRecycleItemTouchListener extends GestureDetector.SimpleOnGestureListener implements RecyclerView.OnItemTouchListener{
    private RecyclerView recyclerView;
    private GestureDetector gestureDetector;

    public OnRecycleItemTouchListener(RecyclerView recyclerView){
        this.recyclerView = recyclerView;
        gestureDetector = new GestureDetector(recyclerView.getContext(),this);
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        gestureDetector.onTouchEvent(e);
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {
        gestureDetector.onTouchEvent(e);
    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
        if (child!=null) {
            RecyclerView.ViewHolder vh = recyclerView.getChildViewHolder(child);
            onItemClick(vh,recyclerView.getChildAdapterPosition(child));
        }
        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        super.onLongPress(e);
        View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
        if (child!=null) {
            RecyclerView.ViewHolder vh = recyclerView.getChildViewHolder(child);
            onItemLongClick(vh,recyclerView.getChildAdapterPosition(child));
        }
    }

    public void onItemLongClick(RecyclerView.ViewHolder vh, int position){}
    public void onItemClick(RecyclerView.ViewHolder vh,int position){}
}

基本上就是将OnItemTouchListener的回调交给SimpleOnGestureListener,然后我们自己开辟回调处理点击和长按事件,只不过要计算出所点击的item。

具体使用:

        recyclerView.addOnItemTouchListener(new OnRecycleItemTouchListener(recyclerView){
            @Override
            public void onItemClick(RecyclerView.ViewHolder vh, int position) {
                super.onItemClick(vh, position);
                mPresenter.recycleClickEvent(position,currentMode,adapter);
            }

            @Override
            public void onItemLongClick(RecyclerView.ViewHolder vh, int position) {
                super.onItemLongClick(vh, position);
                mPresenter.recycleLongPressEvent(currentMode,adapter);
            }
        });

这种方法有一个缺点是只能处理触摸点击事件,有些机器上是按键点击的就处理不了,此时我们可以看下一个方法

2.为View添加点击事件

这种方法就比较简单而且多样了,基本就是对ViewHolder的itemView添加点击事件的监听,既可以在onCreateViewHolder中刚创建view时添加也可以在onBindViewHolder中通过holder.itemView获取到整个item的view设置点击事件或者view中单个元素设置点击事件。简单举一个例子(均是在adapter中添加代码):

public void onBindViewHolder(MyViewHolder myViewHolder, final int position) {
    if( mOnItemClickListener!= null){
        myViewHolder.itemView.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onClick(position);
            }
        });
        myViewHolder.itemView.setOnLongClickListener( new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                mOnItemClickListener.onLongClick(position);
                return false;
            }
        });
    }
}
public interface OnItemClickListener{
        void onClick( int position);
        void onLongClick( int position);
     }
public void setOnItemClickListener(OnItemClickListener onItemClickListener ){
        this. mOnItemClickListener=onItemClickListener;
     }
上一篇 下一篇

猜你喜欢

热点阅读