Reputation: 47
I have a Listener to detect gesture in a RecycleView. It can detect Swipes and (supposedly) ItemTouch. I need the listener to be able to get position from an item on the RecycleView.
Here's the Listener code :
public abstract class OnSwipeTouchListener implements View.OnTouchListener {
private final GestureDetector gestureDetector;
public OnSwipeTouchListener(Context ctx) {
gestureDetector = new GestureDetector(ctx, new GestureListener());
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends GestureDetector.SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 200;
private static final int SWIPE_VELOCITY_THRESHOLD = 200;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
onItemTouch(e.getX(), e.getY());
return true;
}
@Override
public void onLongPress(MotionEvent e) {
onItemLongTouch(e.getX(), e.getY());
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
final int SWIPE_THRESHOLD = 300;
final int SWIPE_VELOCITY_THRESHOLD = 500;
boolean result = false;
try {
float diffY = e1.getY() - e2.getY();
float diffX = e1.getX() - e2.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
} else {
}
result = true;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public void onSwipeTop() {
}
public void onSwipeBottom() {
}
public void onItemTouch(float x, float y) {
}
public void onItemTouch() {
}
public abstract void onTouch(View view, int i);
public abstract void onTouch(View view);
public void onItemLongTouch(float x, float y) {
}
public void onItemLongTouch() {
}
}
And here's the usage of the Listener on my Activity :
recyclerView.setOnTouchListener(new OnSwipeTouchListener(this) {
@Override
public void onSwipeLeft() {
}
@Override
public void onTouch(View view, int i) {
}
@Override
public void onSwipeRight() {
Intent intentSettings = new Intent(NewsActivity.this, TrackingActivity.class);
startActivity(intentSettings);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left); }
@Override
public void onTouch(View view) {
}
@Override
public void onItemLongTouch(float x, float y) {
}
});
If there's anything needed for the answer, just ask me.
Thank you :)
Upvotes: 2
Views: 322
Reputation: 1608
You can get position in onTouch Method using below code
public boolean onTouch(View v, MotionEvent event) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
int pos = view.getChildAdapterPosition(childView)
return gestureDetector.onTouchEvent(event);
}
Please use below code,I have created new class for detecting gestures.
public class RecyclerItemClickListenerTouchs implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
private int pos = -1;
public interface OnItemClickListener {
public void onItemClick(View view, int position);
public void onItemClick(int position);
public void onSwipeRight(int position);
public void onSwipeLeft(int position);
}
GestureDetector mGestureDetector;
public RecyclerItemClickListenerTouchs(Context context, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
mListener.onItemClick(pos);
return true;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
final int SWIPE_THRESHOLD = 300;
final int SWIPE_VELOCITY_THRESHOLD = 500;
boolean result = false;
try {
float diffY = e1.getY() - e2.getY();
float diffX = e1.getX() - e2.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
mListener.onSwipeRight(pos);
} else {
mListener.onSwipeLeft(pos);
}
} else {
}
result = true;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null ) {
mGestureDetector.onTouchEvent(e);
pos = view.getChildAdapterPosition(childView);
}
return false;
}
@Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}}
And in your activity implement RecyclerItemClickListenerTouchs.OnItemClickListener
and use listener with recycler view as rvJobs.addOnItemTouchListener(new RecyclerItemClickListenerTouchs(MyJobsActivity.this, this));
Hope this will help
Upvotes: 0