Search code examples
androidandroid-recyclerviewparallaxandroid-appbarlayout

Content above RecyclerView with parallax effect in Android


As described in 1, I want to implement a Layout where half the screen is some Content, f.e. a LinearLayout including a pie chart. The other half of the screen should be a RecyclerView including a CardView which I already implemented. When scrolling up there should be a Parallax Effect hiding the Contentup to the AppBar.

I tried to use LinearLayouts (where one includes the Content and one includes the Recyclerview) with a weightSum but it did not work properly.

How can I implement this properly?

Design that I want to implement in my App


Solution

  • I agree with the other answer here. Take a look at the docs.

    But for a quick fix,

    First, import the android Design Library

    implementation 'com.android.support:design:28.0.0'
    

    You must already be having the CardView and RecyclerView libraries

    implementation 'com.android.support:cardview-v7:28.0.0'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    

    Then implement this code

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".Main3Activity">
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/Base.ThemeOverlay.AppCompat.Dark.ActionBar"
            android:fitsSystemWindows="true">
    
            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing"
                android:layout_width="match_parent"
                android:layout_height="350dp"
                android:fitsSystemWindows="true"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                app:contentScrim="#0e0d0e"
                app:expandedTitleTextAppearance="@android:color/transparent">
    
                <!--these views are only to illustrate the imp. tags -->
                <!--that you need to implement in your views-->
                <!--in the "Component" area-->
    
                <!--start example-->
    
                <ImageView
                    android:id="@+id/iv"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:contentDescription="@null"
                    android:scaleType="centerCrop"
                    app:layout_collapseMode="parallax"/>
    
                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    app:title="Title"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                    app:layout_collapseMode="parallax"/>
    
                <!--end example-->
    
            </android.support.design.widget.CollapsingToolbarLayout>
    
        </android.support.design.widget.AppBarLayout>
    
    
        <android.support.v4.widget.NestedScrollView
            android:id="@+id/nestedScrollView"
            android:clipToPadding="false"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="1200dp">
    
                <!--implement the RecyclerView here-->
    
                <android.support.v7.widget.RecyclerView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:scrollbars="vertical">
    
                </android.support.v7.widget.RecyclerView>
    
    
            </LinearLayout>
    
        </android.support.v4.widget.NestedScrollView>
    
    </android.support.design.widget.CoordinatorLayout>