java–Recycler视图 – 滚动时调整项目视图的大小(对于类似旋转木马的效果)

java–Recycler视图 – 滚动时调整项目视图的大小(对于类似旋转木马的效果),第1张

概述我需要创建一个垂直的Recyclerview,其中应调整屏幕中心的项目视图,以便在滚动时具有缩放效果.我尝试但没有奏效的事情:>添加滚动侦听器并按位置循环浏览项目视图,测量居中位置,然后更新居中视图的LayoutParams.>RecyclerView不会在滚动时计算项目的位置或更新视图.如果在onScrol

我需要创建一个垂直的RecyclervIEw,其中应调整屏幕中心的项目视图,以便在滚动时具有缩放效果.

我尝试但没有奏效的事情:

>添加滚动侦听器并按位置循环浏览项目视图,测量居中位置,然后更新居中视图的LayoutParams.

> RecyclerVIEw不会在滚动时计算项目的位置或更新视图.如果在onScrolled中执行此类 *** 作,则抛出IllegalStateException

>在onScrollStateChanged中更改居中项目视图的LayoutParams,而滚动状态为IDLE或SETTliNG.

>仅在滚动已经/将要完成之后才更新视图,而不是在正在执行滚动项目期间.

>剩下的最后一个选项是实现自定义的LayoutManager,它将扩展默认的LayoutManager.

>据我所知,实现自定义Layoutmanager涉及处理需要处理的更复杂的计算.

任何其他解决方案或想法将不胜感激.

解决方法:

我找到了this answer on SO,横向完全相同. Answer提供了一个扩展linearlayoutmanager的工作解决方案.我修改了一下以适应垂直列表并且它可以工作.如果实施中有任何错误,请在评论中告诉我.干杯!

自定义布局管理器

public class CenterZoomLayoutManager extends linearlayoutmanager {    private final float mShrinkAmount = 0.15f;    private final float mShrinkdistance = 0.9f;    public CenterZoomLayoutManager(Context context) {        super(context);    }    public CenterZoomLayoutManager(Context context, int orIEntation, boolean reverseLayout) {        super(context, orIEntation, reverseLayout);    }    @OverrIDe    public int scrollVerticallyBy(int dy, RecyclerVIEw.Recycler recycler, RecyclerVIEw.State state) {        int orIEntation = getorIEntation();        if (orIEntation == VERTICAL) {            int scrolled = super.scrollVerticallyBy(dy, recycler, state);            float mIDpoint = getHeight() / 2.f;            float d0 = 0.f;            float d1 = mShrinkdistance * mIDpoint;            float s0 = 1.f;            float s1 = 1.f - mShrinkAmount;            for (int i = 0; i < getChildCount(); i++) {                VIEw child = getChildAt(i);                float childMIDpoint =                        (getDecoratedBottom(child) + getDecoratedtop(child)) / 2.f;                float d = Math.min(d1, Math.abs(mIDpoint - childMIDpoint));                float scale = s0 + (s1 - s0) * (d - d0) / (d1 - d0);                child.setScaleX(scale);                child.setScaleY(scale);            }            return scrolled;        } else {            return 0;        }    }    @OverrIDe    public int scrollHorizontallyBy(int dx, RecyclerVIEw.Recycler recycler, RecyclerVIEw.State state) {        int orIEntation = getorIEntation();        if (orIEntation == HORIZONTAL) {            int scrolled = super.scrollHorizontallyBy(dx, recycler, state);            float mIDpoint = getWIDth() / 2.f;            float d0 = 0.f;            float d1 = mShrinkdistance * mIDpoint;            float s0 = 1.f;            float s1 = 1.f - mShrinkAmount;            for (int i = 0; i < getChildCount(); i++) {                VIEw child = getChildAt(i);                float childMIDpoint =                        (getDecoratedRight(child) + getDecoratedleft(child)) / 2.f;                float d = Math.min(d1, Math.abs(mIDpoint - childMIDpoint));                float scale = s0 + (s1 - s0) * (d - d0) / (d1 - d0);                child.setScaleX(scale);                child.setScaleY(scale);            }            return scrolled;        } else {            return 0;        }    }}

水平方向:


垂直方向:

总结

以上是内存溢出为你收集整理的java – Recycler视图 – 滚动时调整项目视图的大小(对于类似旋转木马的效果)全部内容,希望文章能够帮你解决java – Recycler视图 – 滚动时调整项目视图的大小(对于类似旋转木马的效果)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1101616.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存