使用另一个监听器获取 RecyclerView 项目位置
Getting RecylerView Item Position using another Listener
我有一个侦听器来检测 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) {
}
});
如果有什么需要回答的,尽管问我。
谢谢:)
您可以使用以下代码在 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);
}
请使用下面的代码,我已经创建了新的 class 来检测手势。
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) {
}}
并在您的 activity 工具中 RecyclerItemClickListenerTouchs.OnItemClickListener
并使用带有回收站视图的侦听器 rvJobs.addOnItemTouchListener(new RecyclerItemClickListenerTouchs(MyJobsActivity.this, this));
希望这会有所帮助
我有一个侦听器来检测 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) {
}
});
如果有什么需要回答的,尽管问我。
谢谢:)
您可以使用以下代码在 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);
}
请使用下面的代码,我已经创建了新的 class 来检测手势。
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) {
}}
并在您的 activity 工具中 RecyclerItemClickListenerTouchs.OnItemClickListener
并使用带有回收站视图的侦听器 rvJobs.addOnItemTouchListener(new RecyclerItemClickListenerTouchs(MyJobsActivity.this, this));
希望这会有所帮助