简体   繁体   English

如何创建像谷歌游戏商店一样的水平动画回收视图? 不常见的水平回收器view

[英]How to create horizontal Animated recyclerview like google play store ? Not common horizontal recyclerview

When a user will scroll Recyclerview from right to left at the same time the first view will disappear with fade-out animation according to scrolling and background view also be parallax like google play store app.当用户同时从右向左滚动 Recyclerview 时,第一个视图将根据滚动消失并淡出 animation,背景视图也像 google play store 应用程序一样具有视差。

It will animated recylerview, "not the normal horizontal recyclerview".它将动画recyclerview,“不是正常的水平recyclerview”。 You can see this in google play, not every time, it appears occasionally你可以在google play中看到这个,不是每次都出现,偶尔会出现

来自 Google Play 商店的屏幕截图

According to what you have written, you basically wanted a horizontal recycler view which when scrolled has a fade animation to the header.根据您所写的内容,您基本上想要一个水平回收器视图,当滚动时,该视图具有从 animation 到 header 的渐变。

I had encountered the same problem and I solved it the following was.我遇到了同样的问题,我解决了以下问题。 I used kotlin, during my development.我在开发过程中使用了 kotlin。

Firstly you need to add a scroll listener to your recycler view, i have made an extension function for scroll首先你需要在你的回收器视图中添加一个滚动监听器,我已经为滚动做了一个扩展 function

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)
        }
    })
}

Now create a divider item decorator for adding the padding to the first item现在创建一个分隔项装饰器,用于将填充添加到第一项

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
                }



        }

    }
}

Now in your XML place add the image that you want as background and place the recycler view on top of that image.现在在您的 XML 位置添加您想要作为背景的图像,并将回收器视图放在该图像的顶部。

Once you have done that you just need to add scroll extension to your recyclerview完成后,您只需将滚动扩展添加到您的回收站视图

 private var overallScroll = 0;

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

remap function i have maded to calculate the proper alpha corresponding to scroll displacement.我已经重新映射 function 来计算与滚动位移相对应的正确 alpha。

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

and yes don't forget to make the recycler view horizontal.是的,不要忘记使回收站视图水平。

You can use RecyclerView with following layout manager您可以将 RecyclerView 与以下布局管理器一起使用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM