使用Android中的GestureDetector处理singleClick和LongPress

我有为RecyclerView处理singleClickListener的类.我正在使用GestureDetector处理点击.如何将LongPressListener添加到同一类中以同时处理它们?

public class RecyclerViewItemClickListener implements RecyclerView.OnItemTouchListener {
    private OnItemClickListener mListener;

    public interface OnItemClickListener {
        public void onItemClick(View view, int position);
    }

    GestureDetector mGestureDetector;

    public RecyclerViewItemClickListener(Context context, OnItemClickListener listener) {
        mListener = listener;
        mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return true;
            }

            @Override
            public boolean onDown(MotionEvent event) {
                // triggers first for both single tap and long press
                return true;
            }

            @Override
            public void onLongPress(MotionEvent event) {
                //...
                super.onLongPress(event);
            }
        });
    }

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

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

我在活动中使用此类的类:

recyclerView.addOnItemTouchListener(
                new RecyclerViewItemClickListener(this, new RecyclerViewItemClickListener.OnItemClickListener() {
                    @Override public void onItemClick(View view, int position) {
                        // TODO Handle item click
                        Log.d(TAG, "item clicket position: " + position);
                    }
                })
        );

解决方法:

如果您想在onInterceptTouchEvent上都处理它们,只需像这样使用它.

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
private View childView;
private RecyclerView viewRecycle;

public interface OnItemClickListener {
    void onItemClick(View view, int position);

    void onLongItemClick(View view, int position);
}

GestureDetector mGestureDetector;

public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
    mListener = listener;
    mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            if (childView != null && mListener != null){
                mListener.onItemClick(childView, viewRecycle.getChildAdapterPosition(childView));
            }
            return true;
        }
        @Override
        public void onLongPress(MotionEvent e) {
            if (childView != null && mListener != null) {
                mListener.onLongItemClick(childView, viewRecycle.getChildAdapterPosition(childView));
            }
            super.onLongPress(e);
        }
    });
}

@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
    AppHelper.AppLogger("inside");
    childView = view.findChildViewUnder(e.getX(), e.getY());
    viewRecycle = view;
    mGestureDetector.onTouchEvent(e);
    return false;
}

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


}

@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

}
上一篇:安卓基础学习——点击事件监听的四种写法


下一篇:Android 进阶答疑:Hook