繁体   English   中英

如何使用偏移量回收器视图使自动平滑滚动?

[英]How to make auto smooth scroll with offset Recycler View?

有这样的问题,我有水平RecyclerView ,其中每个单元格小于屏幕的宽度。

所以我在这里找到了解决方案

RecyclerVIew自动滚动以显示所有元素,如新闻源等。

如果一个单元格占据屏幕的整个宽度,否则所有工作都非常好(否则,如果每个单元格占据屏幕宽度的95%),则每次自动滑动都会将该单元格放置在屏幕的开始位置(右侧),这很合逻辑。 因此,在一个可见单元格的末尾是另一单元格的开始

在此处输入图片说明

它看起来不太好。 我需要这样的单元格位于屏幕的中间。

我需要查看上一个单元格-当前-下一个

在此处输入图片说明

现在,我想向您解释一些魔术,如何使当前的平滑滚动(如我在上面的链接中提到的)

我的CustomLinnearLayoutManager此方法

@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position)
{
    LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext())
    {
        @Override
        public PointF computeScrollVectorForPosition(int targetPosition)
        {
            return SmoothLayoutManager.this.computeScrollVectorForPosition(targetPosition);
        }

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

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

但是这种方法没有偏移

我发现了另一种可以提供所需偏移量的方法

scrollToPositionWithOffset(final int position, final int offset)

它看起来确实正是我所需要的,但是这种方法无需平滑的动画即可工作。

所以,最终我的问题是:如何将动画逻辑从第一种方法应用到第二种方法(带偏移的方法)

随便问

要自动捕捉并在RecyclerView中心显示一个项目,只需要使用LinearSnapHelper ,如下所示:

LinearSnapHelper snapHelper = new LinearSnapHelper();
snapHelper.attachToRecyclerView(recyclerView);

如果要以编程方式滚动到特定项目, LinearSnapHelper处理捕捉功能。

SmoothScroller smoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {
        @Override
        protected int getVerticalSnapPreference() {
            return LinearSmoothScroller.SNAP_TO_ANY;
        }

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

...

smoothScroller.setTargetPosition(position);
recyclerView.getLayoutManager().startSmoothScroll(smoothScroller);

这是视觉效果:

.................. 手动滚动 ...........................以编程方式滚动 。 .........

例例

最终,我发现了非常感谢@aminography的方式,他的回答也给了我很多帮助

https://stackoverflow.com/a/39654328

其实现在我有这样的实现

我的自定义LinnearLayoutManager实现

public class SmoothLayoutManager extends LinearLayoutManager
{
public static final int X_25 = 25;
public static final int X_200 = 200;
public static final float DEFAULT = X_25;

/**
 * !! IMPORTANT !!
 * If you need to add new value, don't forget add it here also
 */
@Retention(RetentionPolicy.SOURCE)
@IntDef({X_25, X_200})
private @interface Speed
{

}

private static float MILLISECONDS_PER_INCH = DEFAULT;

public SmoothLayoutManager(Context context)
{
    super(context);
}

public SmoothLayoutManager(Context context, int orientation, boolean reverseLayout)
{
    super(context, orientation, reverseLayout);
}

public SmoothLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
{
    super(context, attrs, defStyleAttr, defStyleRes);
}

public SmoothLayoutManager setSpeedOfSmooth(@Speed int iSpeed)
{
    MILLISECONDS_PER_INCH = iSpeed;

    return this;
}

@Override
public void scrollToPositionWithOffset(final int position, final int offset)
{
    super.scrollToPositionWithOffset(position, offset);
}

@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position)
{
    RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(recyclerView.getContext())
    {
        @Override
        public PointF computeScrollVectorForPosition(int targetPosition)
        {
            return SmoothLayoutManager.this.computeScrollVectorForPosition(targetPosition);
        }

        @Override
        protected int getVerticalSnapPreference()
        {
            return LinearSmoothScroller.SNAP_TO_ANY;
        }

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

        @Override
        public int calculateDtToFit(final int viewStart, final int viewEnd, final int boxStart, final int boxEnd, final int snapPreference)
        {
            return (boxStart + (boxEnd - boxStart) / 2) - (viewStart + (viewEnd - viewStart) / 2);
        }
    };

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

这就是我的设定

private void setRv(Context iC)
    {
        RecyclerView.Adapter adapter = new UpSaleInnerAdapter(mPicasso, mInflater, iLink -> mListener.onButtonClick(iLink));

        mRv.setLayoutManager(new SmoothLayoutManager(iC, LinearLayoutManager.HORIZONTAL, false).setSpeedOfSmooth(SmoothLayoutManager.X_200));
        mRv.setAdapter(adapter);

        SnapHelper snapHelper = new LinearSnapHelper();
        snapHelper.attachToRecyclerView(mRv);
    }

注意 :

我注意到有时您快速滑动一下,所以SnapHelper有些混乱,并传递更多需要的单元格,例如涡轮模式:)

如果有人会找到解决方法,请告诉我。

谢谢!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM