Last active
March 3, 2019 07:32
-
-
Save ashmeh6/915099545b96a536ad78a75b3b16daa6 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.v7.widget.RecyclerView; | |
| import android.view.GestureDetector; | |
| import android.view.MotionEvent; | |
| import android.view.View; | |
| /** | |
| * Created by Ashish on 25-Jul-16. | |
| */ | |
| /* How to Use it | |
| * | |
| * mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getContext(), mRecyclerView, new RecyclerItemClickListener.ClickListener() { | |
| * @Override | |
| * public void onItemClick(View view, int position) { | |
| * //click event here | |
| * } | |
| * @Override | |
| * public void onLongItemClick(View view, int position) { | |
| * //On Long press event here | |
| * } | |
| * })); | |
| * | |
| */ | |
| public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener { | |
| private ClickListener mListener; | |
| public static interface ClickListener { | |
| public void onItemClick(View view, int position); | |
| public void onLongItemClick(View view, int position); | |
| } | |
| GestureDetector mGestureDetector; | |
| public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, ClickListener listener) { | |
| mListener = listener; | |
| mGestureDetector = 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 && mListener != null) { | |
| mListener.onLongItemClick(child, recyclerView.getChildAdapterPosition(child)); | |
| } | |
| } | |
| }); | |
| } | |
| @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.getChildAdapterPosition(childView)); | |
| return true; | |
| } | |
| return false; | |
| } | |
| @Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { } | |
| @Override | |
| public void onRequestDisallowInterceptTouchEvent (boolean disallowIntercept){} | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment