我正在尝试移动和成长一个RecyclerView,以便内容占据触摸输入的全屏。我希望RecyclerView能够保持向左和向右滚动的功能。
我无法让GestureDetector与RecyclerView一起正常工作。捕获onScrollChange不起作用,因为它可能无法滚动。我尝试onTouchEvent,但结果相当错误。有没有人有任何建议?
回购: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;
}
});
答案 0 :(得分:2)
在一个班级中移动以下代码。 {
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() {
}
});
}
使用此听众如下所述。 将Touch侦听器设置为行文件的父视图。 {
{{1}}
}