我有一个处理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);
}
})
);
答案 0 :(得分:3)
如果你想同时处理onInterceptTouchEvent,就这样使用它。
{{1}}