Created
November 9, 2017 02:48
-
-
Save Lzyct/01c17507fdb54c970778915e868e62e3 to your computer and use it in GitHub Desktop.
Recyclerview touch Listener
This file contains hidden or 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 interface ClickListener { | |
| void onClick(View view, int position); | |
| void onLongClick(View view, int position); | |
| } |
This file contains hidden or 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 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) { | |
| } | |
| } |
This file contains hidden or 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
| 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