中的MotionEvent传递给GestureDetectorC

中的MotionEvent传递给GestureDetectorC

本文介绍了将RecyclerView.OnItemTouchListener中的MotionEvent传递给GestureDetectorCompat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实现RecyclerView.OnItemTouchListener的Fragment。如何将单击和长按动作事件从RecyclerView传递到GestureDetectorCompat。这就是我的意思是我只想处理点击和长按,其他事件应该由RecyclerView处理,因为它会正常发生。如何设置?

I have a Fragment that implemets RecyclerView.OnItemTouchListener. How do I pass click and long-click motion events only from the RecyclerView to the GestureDetectorCompat. That is I mean I only want to handle clicks and long-clicks, rest of the events should be handled by the RecyclerView as it would happen normally. How can I set this up?

public class MyFragment extends Fragment implements RecyclerView.OnItemTouchListener,
        GestureDetector.OnGestureListener {

    protected RecyclerView recyclerView;
    protected RecyclerView.Adapter adapter;
    protected LinearLayoutManager layoutManager;
    private GestureDetectorCompat detector;

    public MyFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.myfrag, container, false);

        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;
    }


    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @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;
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent event) {
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView recyclerView, MotionEvent event) {

    }
}


推荐答案

你必须初始化 GestureDetectorCompat onCreateView()方法:

@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 是你的自己的内部类扩展 SimpleOnGestureListener (提供 OnGestureListener 方法的空实现)

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);
    }
}

现在查看行(来自 onCreateView()方法):

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

recyclerView.addOnItemTouchListener(this);

在我们的案例中,'this'是 OnItemTouchListener 包含我们需要实现的两个方法:

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

您只需处理来自 RecyclerView 的单击和长按事件。

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

这篇关于将RecyclerView.OnItemTouchListener中的MotionEvent传递给GestureDetectorCompat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:47