通过触摸或手势识别移动RecyclerView [英] Moving a RecyclerView via Touch or Gesture Recognition
问题描述
我正在尝试移动和生长RecyclerView,以使内容基于触摸输入全屏显示.我希望RecyclerView保持向左和向右滚动的功能.
I am attempting to move and grow, a RecyclerView so that the contents take up the full screen base on touch input. I want the RecyclerView to maintain the ability to scroll left and right.
我无法使GestureDetector与RecyclerView一起正常使用.捕获onScrollChange不起作用,因为它可能无法滚动.我尝试了onTouchEvent,但结果却有很多问题.有人有什么建议吗?
I was unable to get a GestureDetector to work properly with the RecyclerView. Catching the onScrollChange doesn't work as it may not have the ability to scroll. I attempting onTouchEvent but the results were rather buggy. Does anyone have any advice?
回购: https://github.com/CubanAzcuy/Animation-Test >
mListView.setOnTouchListener(new View.OnTouchListener() {
Float mHistoricX = null;
Float mHistoricY = null;
Float mHistoricX2 = null;
Float mHistoricY2 = null;
int mScrollDirection = 0;
//1 = Left Right
//2 = Up Down
@Override
public boolean onTouch(View v, MotionEvent e) {
Log.d("TAG", "eX: " + e.getX() + " eY: " + e.getY());
switch (e.getAction()) {
case MotionEvent.ACTION_UP:
Log.d("TAG", "ACTION_UP");
mHistoricX = null;
mHistoricY = null;
mScrollDirection = 0;
break;
case MotionEvent.ACTION_MOVE:
Log.d("TAG", "ACTION_MOVE");
if(mHistoricX == null || mHistoricY == null) {
mHistoricX = e.getX();
mHistoricY = e.getY();
} else {
if(mScrollDirection == 0) {
float tempX = Math.abs(mHistoricX - e.getX());
float tempy = Math.abs(mHistoricY - e.getY());
if(tempX >= tempy) {
mScrollDirection = 1;
} else {
mScrollDirection = 2;
}
mHistoricX2 = mHistoricX - e.getX();
mHistoricY2 = mHistoricY - e.getY();
} else {
mHistoricX2 = mHistoricX - e.getX();
mHistoricY2 = mHistoricY - e.getY();
Log.d("TAG", "X: " + mHistoricX2 + " Y: " + mHistoricY2);
mHistoricX = e.getX();
mHistoricY = e.getY();
}
}
break;
default:
break;
}
if(mScrollDirection == 2){
mListView.animate().setDuration(0).xBy(-mHistoricX2).yBy(-mHistoricY2);
return true;
}
return false;
}
});
推荐答案
将以下代码移到一个类中. {
Move Below Code in One class. {
public 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 = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
onClick();
return super.onSingleTapUp(e);
}
@Override
public boolean onDoubleTap(MotionEvent e) {
onDoubleClick();
return super.onDoubleTap(e);
}
@Override
public void onLongPress(MotionEvent e) {
onLongClick();
super.onLongPress(e);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.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();
}
}
result = true;
}
else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom();
} else {
onSwipeTop();
}
}
result = true;
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public void onSwipeTop() {
}
public void onSwipeBottom() {
}
public void onClick() {
}
public void onDoubleClick() {
}
public void onLongClick() {
}
}
}
使用此侦听器,如下所述. 将触摸监听器设置为行文件的父视图. {
Use This Listener as Mentioned Below. Set Touch listener to your row file's parent view. {
holder.relMainOfRow.setOnTouchListener(new OnSwipeTouchListener(getActivity()) {
@Override
public void onClick() {
super.onClick();
}
@Override
public void onLongClick() {
super.onLongClick();
}
public void onSwipeTop() {
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public void onSwipeBottom() {
}
});
}
这篇关于通过触摸或手势识别移动RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!