通過(guò)addOnItemTouchListener
import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
public abstract class OnItemTouchListener implements RecyclerView.OnItemTouchListener {
private GestureDetectorCompat mGestureDetectorCompat;
private RecyclerView mRecyclerView;
public OnItemTouchListener(RecyclerView recyclerView) {
mRecyclerView = recyclerView;
mGestureDetectorCompat = new GestureDetectorCompat(mRecyclerView.getContext(),
new MyGestureListener());
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
mGestureDetectorCompat.onTouchEvent(e);
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
mGestureDetectorCompat.onTouchEvent(e);
return false;
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
public abstract void onItemClick(RecyclerView.ViewHolder vh);
public abstract void onItemLongPressClick(RecyclerView.ViewHolder vh);
private class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onSingleTapUp(MotionEvent e) {
View childe = mRecyclerView.findChildViewUnder(e.getX(), e.getY());
if (childe != null) {
RecyclerView.ViewHolder VH = mRecyclerView.getChildViewHolder(childe);
onItemClick(VH);
}
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View childe = mRecyclerView.findChildViewUnder(e.getX(), e.getY());
if (childe != null) {
RecyclerView.ViewHolder VH = mRecyclerView.getChildViewHolder(childe);
onItemLongPressClick(VH);
}
}
}
}
recyclerView.addOnItemTouchListener(new OnItemTouchListener(rcv1) {
@Override
public void onItemClick(RecyclerView.ViewHolder VH) {
Log.i("RecyclerView:", "onItemClick");
}
@Override
public void onItemLongPressClick(RecyclerView.ViewHolder VH) {
Log.i("RecyclerView:", "onItemLongPressClick");
}
});