Last active
February 1, 2019 08:05
-
-
Save Suleiman19/342b872e253c239935c0 to your computer and use it in GitHub Desktop.
Small Util class to set a click listener for RecyclerViews
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 CustomRecyclerClickListener implements RecyclerView.OnItemTouchListener { | |
private OnItemClickListener mListener; | |
public interface OnItemClickListener { | |
public void onItemClick(View view, int position); | |
} | |
GestureDetector mGestureDetector; | |
public CustomRecyclerClickListener(Context context, OnItemClickListener listener) { | |
mListener = listener; | |
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { | |
@Override public boolean onSingleTapUp(MotionEvent e) { | |
return true; | |
} | |
}); | |
} | |
@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.getChildPosition(childView)); | |
return true; | |
} | |
return false; | |
} | |
@Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { } | |
@Override | |
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { | |
} | |
} |
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
recyclerView.addOnItemTouchListener(new CustomRecyclerClickListener(this, new CustomRecyclerClickListener.OnItemClickListener() { | |
@Override | |
public void onItemClick(View view, int position) { | |
/** | |
* TODO: Handle item click here | |
*/ | |
} | |
} | |
}) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment