2
我正在嘗試移動和生長一個RecyclerView,以便內容佔據觸摸輸入的全屏幕基礎。我想讓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;
}
});
享受代碼....... PLZ最多投票如果妳發現有用的:) –