user5102612
user5102612

Reputation: 121

Parallax scrolling background recyclerview

My recyclerview has a custom designed background, basically to add some design noise (like in whatsapp). What I want to achieve is that when I scroll the recyclerview, the background scrolls with it at half speed of the items in the recyclerview.

Any idea if this is possible and if, how to go about it?

Upvotes: 2

Views: 2355

Answers (1)

DEEPANKUR SADANA
DEEPANKUR SADANA

Reputation: 126

This is pretty much achievable; however suppose your recycler view can scroll vertically by 10,000 pixels , would you want your background to be translated by 5000 pixels ? if this is the the then you will have to use a very heavy image file which in your case will most definiatly run out of memory. Otherwise keep a ScrollView and a recycler view inside a frameLayout. Then in your activity/fragment add a scrollChange listener to your recyclerView (this will tell you the displacment of your recycler view,particularly you need dy ). According to the verticall displacement you can scroll the scroll view like this scrollView.smoothScrollBy(0,dy); The xml code code look like this:

    <?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.

Upvotes: 2

Related Questions