gpt4 book ai didi

android - 自定义布局管理器滚动/动画

转载 作者:行者123 更新时间:2023-11-30 00:04:51 25 4
gpt4 key购买 nike

我想做什么。

使用 RecyclerView 创建一个简单的轮播。

问题

  1. 最初 View 没有对齐到中心并且 View 没有达到我想要的样式。(即,完全可见的项目应该比其他项目大,当用手指滚动时它工作正常)
  2. 以编程方式滚动时, View 不会像用手指滚动时那样获得对齐效果。

例如,请参见下面随附的 gif

enter image description here

问题

  1. 如何在开始时获得预期的样式(即完全可见的项目更大)。
  2. 单击滚动到按钮时如何获取样式。 (它滚动到正确的位置,唯一的问题是没有获得预期的样式并且它没有对齐到中心)

完整代码在 github

这是自定义 LayoutManager 的代码

open class CarouselLayoutManager(
context: Context,
orientation: Int,
reverseLayout: Boolean
) : LinearLayoutManager(context, orientation, reverseLayout) {

private val mShrinkAmount = 0.15f
private val mShrinkDistance = 0.9f

override fun onLayoutChildren(recycler: RecyclerView.Recycler?, state: RecyclerView.State?) {
scrollVerticallyBy(0, recycler, state)
super.onLayoutChildren(recycler, state)
}

override fun scrollHorizontallyBy(dx: Int, recycler: RecyclerView.Recycler?, state: RecyclerView.State?): Int {
val orientation = orientation
if (orientation == LinearLayoutManager.HORIZONTAL) {
val scrolled = super.scrollHorizontallyBy(dx, recycler, state)

val midpoint = width / 2f
val d0 = 0f
val d1 = mShrinkDistance * midpoint
val s0 = 1f
val s1 = 1f - mShrinkAmount
for (i in 0 until childCount) {
val child = getChildAt(i)
val childMidpoint = (getDecoratedRight(child) + getDecoratedLeft(child)) / 2f
val d = Math.min(d1, Math.abs(midpoint - childMidpoint))
val scale = s0 + (s1 - s0) * (d - d0) / (d1 - d0)
child.scaleX = scale
child.scaleY = scale
}
return scrolled
} else {
return 0
}
}

override fun scrollVerticallyBy(dy: Int, recycler: RecyclerView.Recycler?, state: RecyclerView.State?): Int {
val orientation = orientation
if (orientation == LinearLayoutManager.VERTICAL) {
val scrolled = super.scrollVerticallyBy(dy, recycler, state)
val midpoint = height / 2f
val d0 = 0f
val d1 = mShrinkDistance * midpoint
val s0 = 1f
val s1 = 1f - mShrinkAmount
for (i in 0 until childCount) {
val child = getChildAt(i)
val childMidpoint = (getDecoratedBottom(child) + getDecoratedTop(child)) / 2f
val d = Math.min(d1, Math.abs(midpoint - childMidpoint))
val scale = s0 + (s1 - s0) * (d - d0) / (d1 - d0)
child.scaleX = scale
child.scaleY = scale
}
return scrolled
} else {
return 0
}
}

最佳答案

最后我用这个库/例子解决了这个问题

  1. DiscreteScrollView
  2. android-viewpager-transformers

这是最终结果。

完整代码见Carousel Demo

enter image description here

关于android - 自定义布局管理器滚动/动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49208783/

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