我的 recyclerview 有一个自定义设计的背景,基本上是为了添加一些设计噪音(就像在 Whatsapp 中一样)。 我想要实现的是,当我滚动 recyclerview 时,背景会以 recyclerview 中项目的一半速度随之滚动。
您知道这是否可行吗?如果可行,该怎么做?
最佳答案
这几乎是可以实现的;但是,假设您的回收器 View 可以垂直滚动 10,000 像素,您是否希望背景平移 5000 像素?如果是这样,那么您将不得不使用非常重的图像文件,在您的情况下,这肯定会耗尽内存。
否则,请在frameLayout 内保留ScrollView 和回收器 View 。
然后在您的 Activity/fragment 中添加一个scrollChange监听器到您的recyclerView(这将告诉您回收器 View 的位移,特别是您需要dy)。
根据垂直位移,您可以像这样滚动 ScrollView scrollView.smoothScrollBy(0,dy);
xml代码代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--your imageView that is bigger than screen height comes here-->
</LinearLayout>
</ScrollView>
<android.support.v7.widget.RecyclerView
android:background="@android:color/transparent"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
</LinearLayout>
Also please feel free to show me your progress and also if you are stuck.
关于android - 视差滚动背景recyclerview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37241313/