【发布时间】:2019-02-04 16:53:27
【问题描述】:
在 RecyclerView 中滚动时,我想实现这样的效果:
中心项的缩放比例大于其他项。我找到了一种缩放所有项目的方法,或者所谓的轮播效果。但这不是我想要的。我只想缩放中心项目,而其他项目保持默认大小。
这是我在其他答案中发现的:
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
val snapHelper = LinearSnapHelper()
snapHelper.attachToRecyclerView(recyclerView)
recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
val layoutManager = recyclerView.layoutManager!!
val midpoint = layoutManager.width / 2f
val distance = shrinkDistance * midpoint
for (i in 0 until layoutManager.childCount) {
val child = layoutManager.getChildAt(i)!!
val childMidpoint = (layoutManager.getDecoratedRight(child) + layoutManager.getDecoratedLeft(child)) / 2f
val d = Math.min(distance, Math.abs(midpoint - childMidpoint))
val scale = 1 + -shrinkAmount * d / distance
child.scaleX = scale
child.scaleY = scale
}
}
})
}
正如我所说,这会缩放所有可见项目,而我只想缩放中心项目。
【问题讨论】:
-
这里的收缩量是多少?
标签: android kotlin android-recyclerview