RecyclerView onDoubleClick和onClick

时间:2014-11-14 15:39:33

标签: android double-click android-recyclerview

RecyclerView onClick question阅读雅各布的解决方案之后,我试图扩展他的“双击”事件的解决方案。当我将recyclelerview注册到click和双击侦听器时,onDoubleClick()被正确调用,但是当用户双击时也会调用onClick(),这是我想要避免的。我希望如果检测到双击事件,则不会检测到单击事件。

以下是改编自雅各布解决方案的双击监听器的代码:

 public class RecyclerItemDoubleClickListener implements RecyclerView.OnItemTouchListener {
    private OnItemDoubleClickListener doubleClickListener;

    public interface OnItemDoubleClickListener {
        public void onItemDoubleClick(View view, int position);
    }

    GestureDetector mGestureDetector;

    public RecyclerItemDoubleClickListener(Context context, OnItemDoubleClickListener listener) {
        doubleClickListener = listener;
        mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override public boolean onDoubleTap(MotionEvent e) {
                return true;
            }
        });
    }

    @Override public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
        View childView = view.findChildViewUnder(e.getX(), e.getY());
        if (childView != null && doubleClickListener != null && mGestureDetector.onTouchEvent(e)) {
            doubleClickListener.onItemDoubleClick(childView, view.getChildPosition(childView));
        }
        return false;
    }

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

我只是这样注册:

    recyclerView.addOnItemTouchListener(new RecyclerItemDoubleClickListener(
            this, new RecyclerItemDoubleClickListener.OnItemDoubleClickListener() {
        @Override
        public void onItemDoubleClick(View view, int position) {
            Toast.makeText(ActivityMain.this, "double click " + position, Toast.LENGTH_SHORT).show();
        }
    }));

    recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(
            this, new RecyclerItemClickListener.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            Toast.makeText(ActivityMain.this, "single click " + position, Toast.LENGTH_SHORT).show();
        }
    }));

你会如何处理?

PS:我还想在同一个Recyclerview项目上独立检测“longClick”,“向左滑动”,“向右滑动”。但我想这可能是我自己编码太多了。我会等待图书馆或谷歌提供它。

2 个答案:

答案 0 :(得分:1)

使用onSingleTapConfirmed()的{​​{1}}方法来区分singleTap和DoubleTap。

http://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener.html#onSingleTapConfirmed%28android.view.MotionEvent%29

要检测向左和向右滑动,请使用GestureListener

onFling()

在onFling中使用以下代码

GestureListener

答案 1 :(得分:0)

您可以使用ItemTouchHelper添加滑动以关闭和拖动&放弃对RecyclerView的支持。