android dev
android dev

Reputation: 218

Android : Recyclerview scrolling with Collapsing toolbar issue

In my app I am using, CollapsingtoolbarLayout together with RecyclerView. Both are working. But I am having a problem with scrolling them. What I want is I want whenever i scroll RecyclerView directly below Collapsing Toobar and I want RecyclerView go up together with toolbar when it is collapsed. but this is not happening whenever i scroll recyclerview first it goes behind corousal then at some point corousal scrolls .But my code is not working as I expected.

following is my code :

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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"
android:fitsSystemWindows="true">


<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    app:layout_behavior="com.google.android.material.appbar.AppBarLayout$Behavior">

    <com.google.android.material.appbar.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/black_theme"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:title="@string/app_name">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/corousal_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <include
                android:id="@+id/layout_sticky_home"
                layout="@layout/item_sticky_header_rt" />

            <androidx.viewpager2.widget.ViewPager2
                android:id="@+id/rvcarousal"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:orientation="horizontal"
                android:visibility="visible"
                app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
                app:layout_constraintDimensionRatio="h,3:4"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/layout_sticky_home" />

            <androidx.appcompat.widget.AppCompatImageView
                android:layout_width="0dp"
                android:layout_height="80dp"
                android:layout_marginBottom="-8dp"
                android:background="@drawable/bg_gradient"
                app:layout_constraintBottom_toBottomOf="@+id/linlay_pager"
                app:layout_constraintEnd_toEndOf="@id/rv_carousal"
                app:layout_constraintStart_toStartOf="@id/rv_carousal" />

            <LinearLayout
                android:id="@+id/linlay_pager"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_below="@+id/rv_carousal"
                android:gravity="center_horizontal"
                android:orientation="horizontal"
                app:layout_constraintBottom_toBottomOf="@id/rv_carousal"
                app:layout_constraintEnd_toEndOf="@id/rv_carousal"
                app:layout_constraintStart_toStartOf="@id/rv_carousal" />

        </androidx.constraintlayout.widget.ConstraintLayout>


    </com.google.android.material.appbar.CollapsingToolbarLayout>

</com.google.android.material.appbar.AppBarLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="@dimen/_20sdp"
        android:clipChildren="false"
        android:orientation="vertical"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
</RelativeLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

i tried many solution but mostly i found in SO's answer to give @string/appbar_scrolling_view_behavior to recyclerview but it wont work

Upvotes: 1

Views: 645

Answers (2)

Meet Miyani
Meet Miyani

Reputation: 119

Try using NestedScrollView as a parent for RecyclerView instead of RelativeLayout

Upvotes: 0

Rakib Hasan
Rakib Hasan

Reputation: 171

I understand the problem, i can suggest you some solution that you can give them a try.

1. Nested Scrolling:

  • You can disable nested scrolling in the RecyclerView and set android:nestedScrollingEnabled="false" on the RecyclerView. This might prevents the RecyclerView from interfering with the CollapsingToolbarLayout's scrolling behavior and also lead to touch events not being handled correctly.

2. AppBarLayout Configuration:

  • Set the app:layout_scrollFlags attribute: Use app:layout_scrollFlags="scroll|exitUntilCollapsed" on the CollapsingToolbarLayout. This ensures the RecyclerView scrolls together with the CollapsingToolbarLayout when it collapses.

hope it helps.

Upvotes: 0

Related Questions