【问题标题】:RecyclerView smoothScrollBy over scrolls the content with on fling gestureRecyclerView smoothScrollBy 使用 on fling 手势滚动内容
【发布时间】:2014-12-12 17:00:45
【问题描述】:

每次在RecyclerView中检测到fling手势,我想平滑向右滚动一个值,通常是recyclerview的宽度(recyclerView的getWidth)。

问题在于,通常使用 onTouchEvent 检测到 fling,它会按 px 量“拖动”视图,然后做出手势 (smoothScrollBy),这意味着视图过度滚动了 px 量。

我试图检测这些 px 的值是什么,但 ACTION_DOWN 似乎没有被调用,所以我很难做到这一点。

我知道“smoothScrollToPosition”,问题是每个项目可能有不同的宽度。

我怎样才能弄清楚如何在每次挥动手势时平滑滚动而不获得额外的像素?

谢谢

@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);
}

【问题讨论】:

  • 发布您当前的代码
  • 添加到第一条评论。我似乎得到的 getX 值与开发人员选项 -> 指针位置不同。
  • 为什么不使用 GestureDetector?
  • 无论如何我都在覆盖 RecyclerView,所以我认为覆盖 fling 会更容易?

标签: android gesture android-recyclerview onfling


【解决方案1】:

这里有一个更简单的技巧,可以在 fling 事件中平滑滚动到某个位置:

@Override
public boolean fling(int velocityX, int velocityY) {

    smoothScrollToPosition(position);
    return super.fling(0, 0);
}

通过调用 smoothScrollToPosition(int position) 覆盖 fling 方法,其中“int position”是您想要在适配器中的视图的位置。您需要以某种方式获得职位的价值,但这取决于您的需求和实施。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-25
    相关资源
    最近更新 更多