Java 如何创建像google play store这样的水平动画回收视图?不常见的水平循环视图
当用户同时从右向左滚动Recyclerview时,第一个视图将消失,并根据滚动显示淡出动画,背景视图也将是类似于谷歌play store应用程序的视差 它将设置recylerview动画,“而不是正常的水平循环视图”。你可以在GooglePlay中看到这一点,不是每次都会出现,偶尔也会出现Java 如何创建像google play store这样的水平动画回收视图?不常见的水平循环视图,java,android,android-recyclerview,android-animation,Java,Android,Android Recyclerview,Android Animation,当用户同时从右向左滚动Recyclerview时,第一个视图将消失,并根据滚动显示淡出动画,背景视图也将是类似于谷歌play store应用程序的视差 它将设置recylerview动画,“而不是正常的水平循环视图”。你可以在GooglePlay中看到这一点,不是每次都会出现,偶尔也会出现 您可以将RecyclerView与以下布局管理器一起使用 LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.th
您可以将RecyclerView与以下布局管理器一起使用
LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManager);
根据您所写的内容,您基本上想要一个水平循环视图,当滚动时,该视图的标题会有一个淡入淡出的动画 我遇到了同样的问题,我解决了它如下。在我的发展过程中,我使用了kotlin 首先你需要添加一个滚动监听器到你的回收视图,我已经为滚动做了一个扩展功能
inline fun RecyclerView.scroll(
crossinline onScrolled: (RecyclerView, Int, Int) -> Unit = { it, dx, dy -> },
crossinline onScrolledChanged: (RecyclerView, Int) -> Unit = { it, newState -> }
) {
addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
onScrolled(recyclerView, dx, dy)
}
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
onScrolledChanged(recyclerView, newState)
}
})
}
现在创建一个分隔项装饰器,用于向第一个项添加填充
class DividerDecorator(val paddingStart: Float) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
super.getItemOffsets(outRect, view, parent, state)
val position = parent.getChildAdapterPosition(view)
if (position == 0 || position == 1) {
outRect.left = paddingStart.toInt()
}
val lp = view.layoutParams as StaggeredGridLayoutManager.LayoutParams
val spanIndex = lp.spanIndex
if (position >= 0) {
if(spanIndex==0) {
outRect.top = 13.px
outRect.bottom = 5.px
}else{
outRect.top = 5.px
outRect.bottom = 13.px
}
}
}
}
现在在您的XML位置添加您想要作为背景的图像,并将recycler视图放置在该图像的顶部
完成后,只需将滚动扩展添加到recyclerview
private var overallScroll = 0;
recycler.scroll(onScrolled = { _, dx, _ ->
overallScroll += dx
backgroundView.animate()
.alpha(overallScroll.remap(padding))
.setInterpolator(LinearInterpolator())
.setDuration(0)
.start()
})
重新映射函数我已经做了计算适当的阿尔法对应的滚动位移
fun Int.remap(offset:Float):Float{
return 1-(this/offset)
}
是的,别忘了让回收者视图水平。你是如何实现这一点的?这是否回答了你的问题?使用ListAdapter和set orientation HORIZONATL的用户ViewPager2。@Abhishekkumar感谢您的回答,但这不是我的解决方案。我想让recyclerview像google play store动画recylerview一样,“而不是普通的水平recyclerview”。你可以在GooglePlay中看到这一点,不是每次都会出现,它偶尔出现。第一个项目很可能不是recyclerview项目。当recyclerview向左滚动时,只需设置动画即可。你试过什么了吗@Mehedihasanthank感谢你的回答,但这不是我的解决方案。我想让recyclerview像google play store动画recylerview一样,而不是普通的水平recyclerview。你可以在google play中看到这一点,但不是每次都会看到,偶尔会出现。play store应用程序屏幕是许多RecyclerViews的集合,根据需要水平或垂直添加到嵌套的滚动视图中。如果需要,可以将动画添加到Recyclerview中