Skip to content

Instantly share code, notes, and snippets.

@Lzyct
Created November 9, 2017 02:48
Show Gist options
  • Select an option

  • Save Lzyct/01c17507fdb54c970778915e868e62e3 to your computer and use it in GitHub Desktop.

Select an option

Save Lzyct/01c17507fdb54c970778915e868e62e3 to your computer and use it in GitHub Desktop.
Recyclerview touch Listener
public interface ClickListener {
void onClick(View view, int position);
void onLongClick(View view, int position);
}
public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
private GestureDetector gestureDetector;
private ClickListener clickListener;
public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {
this.clickListener = clickListener;
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null) {
clickListener.onLongClick(child, recyclerView.getChildPosition(child));
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildPosition(child));
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
// clickListener.motionEvent(e);
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
yourRecyclerView.addOnItemTouchListener(new RecyclerTouchListener(this, yourRecyclerView, new ClickListener() {
@Override
public void onClick(View view, int position) {
}
@Override
public void onLongClick(View view, int position) {
}
@Override
public void motionEvent(MotionEvent event) {
}
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment