簡體   English   中英

Android:控制在回收站視圖上平滑滾動

[英]Android : Control Smooth scroll over recycler view

我正在將 Recyclerview 與 CardView 一起使用。 我知道如何控制列表視圖上的速度。 但不適用於 Recyclerview。

我在找到的類名SmoothScroll 中搜索了很多。 怎么用那個? 我不知道! 現在 Recyclerview 默認滾動速度很快。

更新:

我用這個總結了吉爾的答案

當你說“ 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 的示例。

計算給定位置的滾動 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);
}

我很難提供更量身定制的解決方案,因為您沒有發布任何代碼,也沒有提供有關您的設置的大量信息,但我希望這涵蓋了大多數基礎,並能幫助您找到最適合您的解決方案。

我只是簡化了 Answer 如何使用它來控制平滑滾動。

創建班級

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

只需實現 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);
    }

};

使用它來平滑滾動

recyclerViewCart.isNestedScrollingEnabled = false

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM