Last active
July 21, 2017 16:23
-
-
Save nate-eisner/b9b2785602d9f4297d61705295f3202b to your computer and use it in GitHub Desktop.
Recycler Touch Handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SomeScreen { | |
private RecyclerView recyclerView; | |
private GestureDetectorCompat gestureDetector; | |
public void bind() { | |
gestureDetector = new GestureDetectorCompat(this, new RecyclerViewGestureListener()); | |
} | |
private void recyclerItemTap(View item) { | |
int position = recyclerView.getChildAdapterPosition(view); | |
if (position != NO_ID) { | |
//handle onClick on the item | |
} | |
} | |
/** | |
* A gesture listener for on top of the recycler view. | |
*/ | |
private class RecyclerViewGestureListener extends GestureDetector.SimpleOnGestureListener { | |
@Override | |
public boolean onSingleTapConfirmed(@NonNull MotionEvent e) { | |
View view = recyclerView.findChildViewUnder(e.getX(), e.getY()); | |
recyclerItemTap(view); | |
return super.onSingleTapConfirmed(e); | |
} | |
@Override | |
public void onLongPress(@NonNull MotionEvent e) { | |
//user long pressed on item | |
super.onLongPress(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment