简体   繁体   中英

Recyclerview does not follow the collapsing toolbar

I created a collapsing toolbar and placed a recyclerview below it but when I run the app the cardviews inside the recycleview overlaps the image of the collapsing toolbar .How can I fix this

MainActivity.xml

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
     <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleMarginEnd="64dp"
            android:fitsSystemWindows="true">

            <ImageView
                android:id="@+id/image"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scaleType="centerCrop"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax"/>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_collapseMode="pin" />

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>




    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/rv"
        android:orientation="vertical"
        android:clipToPadding="false"
        android:layout_marginTop="?attr/actionBarSize"
        android:layout_below="@+id/image"
        />
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        app:layout_anchor="@id/app_bar_layout"
        style="@style/fab"
        app:theme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_anchorGravity="bottom|right|end"
        />




</android.support.design.widget.CoordinatorLayout>

Item.xml (layout containing the cardview inside the recyclerview)
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.co nbm/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="6dp"
    app:cardElevation="6dp"
    android:padding="6dp"

    android:id="@+id/cv">

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.co nbm/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:background="#4682b4"
        android:padding="16dp"
        >


        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:scaleType="centerCrop"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="16dp"
            android:src="@drawable/li"
            android:id="@+id/imageView" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView"
            android:text="Aayush Chaubey"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/imageView"
            android:textColor="#ffffff"
            android:textSize="30sp"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView2"
            android:text="app developer"
            android:textColor="#ffffff"
            android:textSize="25sp"
            android:layout_centerVertical="true"
            android:layout_alignLeft="@+id/textView"
            android:layout_alignStart="@+id/textView" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Show Capsule"
            android:textColor="#00bfff"
            android:textSize="20sp"
            android:layout_marginBottom="10dp"
            android:layout_marginRight="10dp"
            android:id="@+id/textView3"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />



    </RelativeLayout>


</android.support.v7.widget.CardView>

Well, I think you need to add this line app:layout_behavior="@string/appbar_scrolling_view_behavior" in your RecyclerView

Eg:

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

Most importantly you will also need to add xmlns:app="http://schemas.android.com/apk/res-auto" in coordinatorLayout tag.

Another point: Make sure you're using com.android.support:recyclerview-v7:22.2.0 with prior version it might not work properly

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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