首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RecyclerView smoothScrollBy使用on fling手势滚动内容

RecyclerView smoothScrollBy使用on fling手势滚动内容
EN

Stack Overflow用户
提问于 2014-12-13 01:00:45
回答 1查看 3.8K关注 0票数 0

每次在回收器视图中检测到投掷手势时,我想平滑向右滚动一个值,通常是回收器视图的宽度(RecyclerView的getWidth)。

问题是,onTouchEvent通常会检测到fling,它会根据px的大小“拖动”视图,然后做出手势(smoothScrollBy),这意味着视图会按px的大小滚动。

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

我知道"smoothScrollToPosition",问题是每一项都可能有不同的宽度。

我如何才能弄清楚如何在不获得额外px的情况下每次在fling手势上执行smoothScroll?

谢谢

代码语言:javascript
复制
@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);
}
EN

回答 1

Stack Overflow用户

发布于 2015-03-13 05:15:16

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

代码语言:javascript
复制
@Override
public boolean fling(int velocityX, int velocityY) {

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

使用对smoothScrollToPosition(int position)的调用覆盖fling方法,其中"int position“是您希望在适配器中的视图的位置。你需要以某种方式获得职位的价值,但这取决于你的需求和实现。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27448494

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档