Created
November 26, 2018 07:34
-
-
Save gsanthosh91/c219efde700b8138c162745a569adc6e to your computer and use it in GitHub Desktop.
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
| import android.content.Context; | |
| import android.support.annotation.NonNull; | |
| import android.support.v7.widget.RecyclerView; | |
| import android.view.GestureDetector; | |
| import android.view.MotionEvent; | |
| import android.view.View; | |
| 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.getChildLayoutPosition(child)); | |
| } | |
| } | |
| }); | |
| } | |
| @Override | |
| public boolean onInterceptTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) { | |
| View child = rv.findChildViewUnder(e.getX(), e.getY()); | |
| if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) { | |
| clickListener.onClick(child, rv.getChildLayoutPosition(child)); | |
| } | |
| return false; | |
| } | |
| @Override | |
| public void onTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) { | |
| } | |
| @Override | |
| public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { | |
| } | |
| public interface ClickListener { | |
| void onClick(View view, int position); | |
| void onLongClick(View view, int position); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment