Passing MotionEvents from RecyclerView.OnItemTouchListener to GestureDetectorCompat

前端 未结 2 1377
醉梦人生
醉梦人生 2020-12-09 00:50

I have a Fragment that implemets RecyclerView.OnItemTouchListener. How do I pass click and long-click motion events only from the RecyclerView to the Gestur

相关标签:
2条回答
  • 2020-12-09 00:53

    You have to initialize GestureDetectorCompat in onCreateView() method:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.myfrag, container, false);
    
        detector = new GestureDetectorCompat(getActivity(), new RecyclerViewOnGestureListener());
    
        recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview);
    
        layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addOnItemTouchListener(this);
    
        adapter = new MyAdapter(myData));
        recyclerView.setAdapter(adapter);
        return rootView;
    }
    

    RecyclerViewOnGestureListener is your own inner class extending SimpleOnGestureListener (that provides empty implementation of OnGestureListener methods)

    private class RecyclerViewOnGestureListener extends SimpleOnGestureListener {
    
        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            View view = recyclerView.findChildViewUnder(e.getX(), e.getY());
            int position = recyclerView.getChildPosition(view);
    
            // handle single tap
    
            return super.onSingleTapConfirmed(e);
        }
    
        public void onLongPress(MotionEvent e) {
            View view = recyclerView.findChildViewUnder(e.getX(), e.getY());
            int position = recyclerView.getChildPosition(view);
    
            // handle long press
    
            super.onLongPress(e);
        }
    }
    

    Now look at line (from onCreateView() method):

    recyclerView.addOnItemTouchListener(this);
    

    In our case 'this' is OnItemTouchListener containing two methods we need to implement:

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

    Here is an explanation what these methods mean: https://developer.android.com/reference/android/support/v7/widget/RecyclerView.OnItemTouchListener.html

    It's all you need to handle single tap and long press events from RecyclerView.

    0 讨论(0)
  • 2020-12-09 01:10

    I might be late but for visual feedback add this to your list_item layout

    android:background="@drawable/tranparent_selector"
    android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    

    Also, onSingleTapUp can be used instead of onSingleTapConfirmed as tapping is usually fast. So if you tap fast to other items you wont get it working. For fast tapping I prefer onSingleTapUp

    0 讨论(0)
提交回复
热议问题