每次在回收器视图中检测到投掷手势时,我想平滑向右滚动一个值,通常是回收器视图的宽度(RecyclerView的getWidth)。
问题是,onTouchEvent通常会检测到fling,它会根据px的大小“拖动”视图,然后做出手势(smoothScrollBy),这意味着视图会按px的大小滚动。
我试图检测这些px的值是什么,但ACTION_DOWN似乎没有被调用,所以我很难做到这一点。
我知道"smoothScrollToPosition",问题是每一项都可能有不同的宽度。
我如何才能弄清楚如何在不获得额外px的情况下每次在fling手势上执行smoothScroll?
谢谢
@Override
public boolean fling(int velocityX, int velocityY) {
Logger.d(TAG, "Fling");
isFirstOnTouchEvent = true;
Logger.d(TAG, "firstX: " + firstX + " lastX: " + lastX);
Logger.d(TAG, "diff? " + (firstX - lastX));
if(velocityX > 0) {
smoothScrollBy(getWidth() - (int) (firstX - lastX), 0);
} else {
smoothScrollBy(-getWidth(), 0);
}
Logger.d(TAG, "\n\n\n");
return true;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
return super.onInterceptTouchEvent(e);
}
private boolean isFirstOnTouchEvent = true;
private double firstX;
private double lastX;
@Override
public boolean onTouchEvent(MotionEvent e) {
Logger.d(TAG, "e: " + e.getAction() + " with raw x: " + e.getRawX() + " with x: " + e.getX());
if(isFirstOnTouchEvent) {
isFirstOnTouchEvent = false;
firstX = e.getX();
}
lastX = e.getX();
return super.onTouchEvent(e);
}发布于 2015-03-13 05:15:16
这里有一个更简单的技巧,可以在fling事件中平滑地滚动到某个位置:
@Override
public boolean fling(int velocityX, int velocityY) {
smoothScrollToPosition(position);
return super.fling(0, 0);
}使用对smoothScrollToPosition(int position)的调用覆盖fling方法,其中"int position“是您希望在适配器中的视图的位置。你需要以某种方式获得职位的价值,但这取决于你的需求和实现。
https://stackoverflow.com/questions/27448494
复制相似问题