关于RecyclerView.setOnClickListener(..)无效的解决方案

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lty406910111/article/details/79239176

直接留干货了

setOnClickListener()是父类的方法,RecyclerView重写了onTouch事件没有去理会父类的Listener,导致OnClickListener失效,

解决方法是:

gestureDetector = new GestureDetector(getContext(), new GestureDetector.OnGestureListener() {
    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        // do something
        return true;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        return false;
    }
});

mRecycler.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }
});

关于GestureDetector,请看这里http://blog.csdn.net/totond/article/details/77881180

阅读更多

没有更多推荐了,返回首页