Android 使用其他侦听器获取RecylerView项位置
我有一个监听器可以在视频中察觉到手势。它可以检测刷卡和(据说)物品触摸。我需要侦听器能够从RecycleView上的项目获取位置 以下是侦听器代码:Android 使用其他侦听器获取RecylerView项位置,android,android-recyclerview,gesturedetector,Android,Android Recyclerview,Gesturedetector,我有一个监听器可以在视频中察觉到手势。它可以检测刷卡和(据说)物品触摸。我需要侦听器能够从RecycleView上的项目获取位置 以下是侦听器代码: public abstract class OnSwipeTouchListener implements View.OnTouchListener { private final GestureDetector gestureDetector; public OnSwipeTouchListener(Context ctx) { ge
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() {
}
}
下面是侦听器在我的活动中的用法:
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);
}
请使用下面的代码,我已经为检测手势创建了一个新类
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) {
}}
在您的活动中,实现回收器单击ListenerTouch.OnItemClickListener
并使用具有回收器视图的侦听器作为rvJobs.addonimtouchListener(新的回收器单击ListenerTouchs(MyJobsActivity.this,this))代码>
希望这会有帮助你不能使用android.support.v7.widget.helper.ItemTouchHelper
?我应该把它放在我的监听器类或我的活动中吗?@bravevctr欢迎你,请让我知道它是否有效