Created
January 16, 2019 21:04
-
-
Save singhangadin/8a3f4e7e9b17224680aecd94d3ec9078 to your computer and use it in GitHub Desktop.
RecyclerView Touch Helper
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
import android.support.v7.widget.RecyclerView; | |
import android.view.View; | |
public interface OnItemClickListener { | |
void onClick(RecyclerView parent, View v, int position); | |
} |
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
import android.support.v7.widget.RecyclerView; | |
import android.view.View; | |
public interface OnItemLongClickListener { | |
void onLongClick(RecyclerView recyclerView, View v, int position); | |
} |
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
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 RecyclerViewTouchHandler implements RecyclerView.OnItemTouchListener { | |
private GestureDetector gestureDetector; | |
private RecyclerView mRecyclerView; | |
private OnItemClickListener clickListener; | |
private OnItemLongClickListener mOnlongClickListener; | |
public RecyclerViewTouchHandler(Context context, RecyclerView recyclerView) { | |
this.mRecyclerView = recyclerView; | |
mRecyclerView.addOnItemTouchListener(this); | |
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { | |
@Override | |
public boolean onSingleTapUp(MotionEvent e) { | |
return true; | |
} | |
@Override | |
public void onLongPress(MotionEvent e) { | |
View child = mRecyclerView.findChildViewUnder(e.getX(), e.getY()); | |
if (child != null && mOnlongClickListener != null) { | |
mOnlongClickListener.onLongClick(mRecyclerView, child, mRecyclerView.getChildAdapterPosition(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(rv, child, rv.getChildAdapterPosition(child)); | |
return true; | |
} | |
return false; | |
} | |
@Override | |
public void onTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) { } | |
@Override | |
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { } | |
public void setOnClickListener(OnItemClickListener clickListener) { | |
this.clickListener = clickListener; | |
} | |
public void setOnlongClickListener(OnItemLongClickListener onlongClickListener) { | |
this.mOnlongClickListener = onlongClickListener; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment