【问题标题】:How to create horizontal Animated recyclerview like google play store ? Not common horizontal recyclerview如何创建像 google play store 一样的水平动画回收视图?不常见的水平回收器view
【发布时间】:2019-11-14 10:20:27
【问题描述】:

当用户同时从右向左滚动 Recyclerview 时,第一个视图将根据滚动消失并带有淡出动画,并且背景视图也像 google play store 应用程序一样具有视差。

它将动画recyclerview,“不是正常的水平recyclerview”。这个在google play里可以看到,不是每次都出现,偶尔出现

【问题讨论】:

标签: java android android-recyclerview android-animation


【解决方案1】:

根据您所写的内容,您基本上想要一个水平回收器视图,当滚动时该视图具有淡入淡出的动画到标题。

我遇到了同样的问题,我解决了以下问题。我在开发过程中使用了 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 位置添加您想要作为背景的图像并将回收器视图放在该图像的顶部。

完成后,您只需将滚动扩展添加到您的回收站视图

 private var overallScroll = 0;

  recycler.scroll(onScrolled = { _, dx, _ ->
                    overallScroll += dx
                    backgroundView.animate()
                            .alpha(overallScroll.remap(padding))
                            .setInterpolator(LinearInterpolator())
                            .setDuration(0)
                            .start()
                })

我已经使用 remap 函数来计算对应于滚动位移的正确 alpha。

fun Int.remap(offset:Float):Float{
    return 1-(this/offset)
}

是的,别忘了将回收站视图设为水平。

【讨论】:

    【解决方案2】:

    您可以将 RecyclerView 与以下布局管理器一起使用

    LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false);
    recyclerView.setLayoutManager(layoutManager);
    

    【讨论】:

    • 感谢您的回答,但这不是我的解决方案。我想制作像 google play store 动画 recyclerview 一样的 recyclerview,而不是普通的水平 recyclerview。不是每次都可以在 google play 中看到,偶尔会出现。
    • Play 商店应用屏幕是许多 RecyclerViews 的集合,这些 RecyclerViews 根据要求水平或垂直添加到嵌套的 ScrollView 中。如果您愿意,可以向 Recyclerview 添加动画,请参阅 stackoverflow.com/questions/35306779/…
    猜你喜欢
    • 2021-11-19
    • 1970-01-01
    • 2021-06-08
    • 1970-01-01
    • 2017-11-30
    • 2018-05-07
    • 1970-01-01
    • 1970-01-01
    • 2018-05-08
    相关资源
    最近更新 更多