我有一个处理RecyclerView单击事件的类。我正在使用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);
}
})
);