gpt4 book ai didi

java - 回收站 View - 滚动时调整项目 View 的大小(用于类似轮播的效果)

转载 作者:IT老高 更新时间:2023-10-28 20:49:23 25 4
gpt4 key购买 nike

我需要创建一个垂直回收 View ,其中屏幕中心的项目 View 应调整大小以在滚动时具有类似缩放的效果。

我尝试过但没用的方法:

  1. 添加一个滚动监听器并按位置循环遍历项目 View ,测量居中位置,然后更新居中 viewLayoutParams

    • RecyclerView 不会在滚动时计算项目的位置或更新 View 。如果在 onScrolled
    • 中执行此类操作,则会抛出 IllegalStateException
  2. 在滚动状态为 IDLESETTLING 时更改 onScrollStateChanged 中居中项目 View 的 LayoutParams

    • 仅在滚动已经/将要完成后更新 View ,而不是在正在执行滚动项目期间。
  3. 剩下的最后一个选项是实现自己的自定义 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;
}

}
}

水平方向: enter image description here

垂直方向:

enter image description here

关于java - 回收站 View - 滚动时调整项目 View 的大小(用于类似轮播的效果),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41307578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com