我有一个侦听器来检测RecycleView中的手势。它可以检测滑动和(据说)ItemTouch。我需要侦听器能够从RecycleView上的项目获取位置。
这是侦听器代码:
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() {
}
}
这是我的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) {
}
});
如果需要答案,请问我。
谢谢:)
答案 0 :(得分:0)
您可以使用以下代码在onTouch方法中获取位置
public boolean onTouch(View v, MotionEvent event) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
int pos = view.getChildAdapterPosition(childView)
return gestureDetector.onTouchEvent(event);
}
请使用以下代码,我创建了新类来检测手势。
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) {
}}
在您的活动中实施RecyclerItemClickListenerTouchs.OnItemClickListener
并将侦听器与回收者视图一起用作rvJobs.addOnItemTouchListener(new RecyclerItemClickListenerTouchs(MyJobsActivity.this, this));
希望这会有所帮助