凡的ListView有onItemClickListener,RecyclerView有addOnItemTouchListener。
您可以將偵聽器提供給活動內的回收器視圖,因此範圍將會使偵聽器能夠引用文本。
你可以使用類似於OnItemTouchListener的實現,它也可以捕獲左右滑動。
public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
private static final String TAG = "RecyclerTouchListener";
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private static final int SWIPE_MAX_OFF_PATH = 250;
private OnTouchActionListener mOnTouchActionListener;
private GestureDetectorCompat mGestureDetector;
public static interface OnTouchActionListener {
public void onLeftSwipe(View view, int position);
public void onRightSwipe(View view, int position);
public void onClick(View view, int position);
}
public RecyclerTouchListener(Context context, final RecyclerView recyclerView,
OnTouchActionListener onTouchActionListener){
mOnTouchActionListener = onTouchActionListener;
mGestureDetector = new GestureDetectorCompat(context,new GestureDetector.SimpleOnGestureListener(){
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Log.d(TAG, "On Single Tap Confirmed");
// Find the item view that was swiped based on the coordinates
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
int childPosition = recyclerView.getChildPosition(child);
mOnTouchActionListener.onClick(child, childPosition);
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2,
float velocityX, float velocityY) {
Log.d(TAG, "onFling: " + e1.toString() + e2.toString());
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
return false;
}
// Find the item view that was swiped based on the coordinates
View child = recyclerView.findChildViewUnder(e1.getX(), e1.getY());
int childPosition = recyclerView.getChildPosition(child);
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Log.d(TAG, "Left Swipe");
if (mOnTouchActionListener != null && child != null) {
mOnTouchActionListener.onLeftSwipe(child, childPosition);
}
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Log.d(TAG, "Right Swipe");
if (mOnTouchActionListener != null && child != null) {
mOnTouchActionListener.onRightSwipe(child, childPosition);
}
}
} catch (Exception e) {
// nothing
}
return false;
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
mGestureDetector.onTouchEvent(e);
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
// do nothing
}
}
然後在你的活動添加你的聽衆一起的
mRecyclerView.addOnItemTouchListener(
new RecyclerTouchListener(getActivity(), mRecyclerView,
new RecyclerTouchListener.OnTouchActionListener() {
@Override
public void onClick(View view, int position) {
}
@Override
public void onRightSwipe(View view, int position) {
}
@Override
public void onLeftSwipe(View view, int position) {
}
}))
[RecyclerView的onClick](可能的重複https://stackoverflow.com/questions/ 24471109/recyclerview-onclick) – Nexen