【问题标题】:Android : Control Smooth scroll over recycler viewAndroid:控制平滑滚动回收器视图
【发布时间】:2015-03-02 05:40:03
【问题描述】:

我将 Recyclerview 与 CardView 一起使用。我知道如何控制列表视图的速度。但不适用于 Recyclerview。

我在找到的类名SmoothScroll 中搜索了很多。怎么用?我不知道!现在 Recyclerview 默认滚动很快。

更新:

我用this总结了吉尔的答案

【问题讨论】:

  • @XaverKapeller 我想在 RecyclerView 中实现慢速滚动。
  • 慢速滚动是什么意思?请进一步解释。您想以编程方式滚动到其他位置吗?
  • 还是不明白。什么滚动速度?你在说什么?以编程方式滚动到另一个位置时的滚动速度?
  • 我的 RecyclerView 有无穷无尽的项目(接近 10000 个项目)。当用户滚动 RecyclerView 时,它的滚动就像风一样无法看到 Items。所以我的想法是减慢滚动速度。我希望现在你明白我想说的。
  • 所以你的意思是当用户抛出RecyclerView或什么?

标签: android android-5.0-lollipop android-support-library android-recyclerview


【解决方案1】:

不清楚您说“smoothScroll”是什么意思。您可能指的是自动滚动到指定位置的自动“smoothScrollToPosition”,您可能是在谈论手动滚动,也可能是在谈论投掷。为了繁荣,我现在将尝试回答所有这些问题。

1.自动平滑滚动。

在您的布局管理器中,您需要实现smoothScrollToPosition 方法:

    @Override
    public void smoothScrollToPosition(RecyclerView recyclerView, State state, int position)
    {
        // A good idea would be to create this instance in some initialization method, and just set the target position in this method.
        LinearSmoothScroller smoothScroller = new LinearSmoothScroller(getContext())
        {
            @Override
            public PointF computeScrollVectorForPosition(int targetPosition)
            {
                int yDelta = calculateCurrentDistanceToPosition(targetPosition);
                return new PointF(0, yDelta);
            }

            // This is the important method. This code will return the amount of time it takes to scroll 1 pixel.
            // This code will request X milliseconds for every Y DP units.
            @Override
            protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics)
            {
                return X / TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, Y, displayMetrics);
            }

        };
        smoothScroller.setTargetPosition(position);

        startSmoothScroll(smoothScroller);
    }

在本例中,我使用了一个名为“calculateCurrentDistanceToPosition”的辅助方法。这可能有点棘手,因为它涉及跟踪您当前的滚动位置,并计算给定 y 位置的滚动位置。您可以找到一个示例,说明如何跟踪回收商的滚动 y here。

计算给定位置的滚动 y 实际上取决于您的回收站显示的内容。假设您的所有物品的高度相同,您可以通过执行以下计算来计算:

targetScrollY = targetPosition * itemHeight

然后,要计算需要滚动的距离,只需用目标滚动 y 减去当前滚动 y:

private int calculateCurrentDistanceToPosition(int targetPosition) {
    int targetScrollY = targetPosition * itemHeight;
    return targetScrollY - currentScrollY;
}

2。减慢手动滚动速度。

再次,您需要编辑布局管理器,这次是 scrollVerticallyBy 方法:

    @Override
    public int scrollVerticallyBy(int delta, Recycler recycler, State state)
    {
       // write your limiting logic here to prevent the delta from exceeding the limits of your list.

       int prevDelta = delta;
       if (getScrollState() == SCROLL_STATE_DRAGGING)
            delta = (int)(delta > 0 ? Math.max(delta * MANUAL_SCROLL_SLOW_RATIO, 1) : Math.min(delta * MANUAL_SCROLL_SLOW_RATIO, -1));  

       // MANUAL_SCROLL_SLOW_RATIO is between 0 (no manual scrolling) to 1 (normal speed) or more (faster speed).
       // write your scrolling logic code here whereby you move each view by the given delta

        if (getScrollState() == SCROLL_STATE_DRAGGING)
            delta = prevDelta;

        return delta;
    }

编辑:在上述方法中,我调用“getScrollState()”。这是RecyclerView的方法。在这个实现中,我的自定义LayoutManager 是我的自定义RecyclerView 的嵌套类。如果这对你不起作用,你可以尝试通过一些界面模式来获取滚动状态。

3.放慢投掷速度

在这里,您想缩小投掷速度。您需要在 RecyclerView 子类中覆盖 fling 方法:

@Override
public boolean fling(int velocityX, int velocityY)
{
     velocityY *= FLING_SCALE_DOWN_FACTOR; // (between 0 for no fling, and 1 for normal fling, or more for faster fling).

     return super.fling(velocityX, velocityY);
}

我很难提供更量身定制的解决方案,因为您没有发布任何代码,也没有提供有关您的设置的太多信息,但我希望这涵盖了大多数基础并帮助您找到最适合您的解决方案.

【讨论】:

  • 我想要的是向下滚动手动滚动您的第二点。但是那里的方法和变量是未定义的。你能更新一下吗?
  • 恐怕为了进一步帮助您,我需要您发布更多代码,以便我了解您在做什么以及您遇到的问题。跨度>
  • 如果不是创建自己的布局管理器,而是给 RecyclerView 一个新的 LinearLayoutManager,滚动问题会停止吗?
  • 您复制了我的代码示例,但没有根据您的需要进行调整...“dy”需要重构为“detla”,MANUAL_SCROLL_SLOW_RATIO 是您必须定义的最终静态变量,并且应该在 0 和 1 之间,当然,您需要实现自己的逻辑来调整所有视图的位置,以及分离/报废不需要的视图。
  • 并计算CurrentDistanceToPosition() ?你从哪里得到这个?
【解决方案2】:

我只是简化回答如何使用它来控制平滑滚动。

创建类

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;

public class CustomRecyclerView extends RecyclerView {

    Context context;

    public CustomRecyclerView(Context context) {
        super(context);
        this.context = context;
    }

    public CustomRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

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

        velocityY *= 0.7;
        // velocityX *= 0.7; for Horizontal recycler view. comment velocityY line not require for Horizontal Mode.

        return super.fling(velocityX, velocityY);
    }

}

通过将 0.7 替换为您的值来调整速度。

现在像这样在你的 XML 中使用这个类。

<<yourpackage>.CustomRecyclerView
    android:id="@+id/my_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical" />

像阅读Java一样的RecyclerView。

CustomRecyclerView mRecyclerView;
mRecyclerView = (CustomRecyclerView) findViewById(R.id.my_recycler_view);

【讨论】:

  • 它对我没有任何作用
  • 你做了什么?按照我在回答中的描述
  • 我也是这样做的。但不知何故将velocityY设置为不同的值(甚至0.1)什么都不做
  • xml和java你改了吗?
  • 对不起,我使用的是水平模式,我只需要使用velocityX而不是velocityY。现在一切都很好。感谢您的反馈 +1
【解决方案3】:

只需实现 LinearLayoutManager 的 smoothScrollToPosition():

LinearLayoutManager layoutManager = new LinearLayoutManager(this) {

    @Override
    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
        LinearSmoothScroller smoothScroller = new LinearSmoothScroller(this) {

            private static final float SPEED = 300f;// Change this value (default=25f)

            @Override
            protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
                return SPEED / displayMetrics.densityDpi;
            }

        };
        smoothScroller.setTargetPosition(position);
        startSmoothScroll(smoothScroller);
    }

};

【讨论】:

  • 这对我帮助很大。感谢您发布!
  • 您可以节省很多时间。
  • 例如,如何让它在 1 秒内平滑滚动到某个位置?
  • 我收到类似“java.lang.IllegalStateException: If you provide an interpolator, you must set a positive duration”之类的错误。它需要持续时间。
  • @Blunderer,你是天才
【解决方案4】:

使用它来平滑滚动

recyclerViewCart.isNestedScrollingEnabled = false

【讨论】:

  • 我建议您将 cmets 添加到您的代码中,因为这将帮助其他成员了解您使用此代码的意图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-27
相关资源
最近更新 更多