-1

I have a fragment with such a layout

    <?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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

    <android.support.design.widget.AppBarLayout
        android:background="?attr/cropperBckColor"
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/cropper_height"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsingToolBarLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:statusBarScrim="@android:color/transparent"
            app:contentScrim="@color/transparent">

            <com.paralect.mediapicker.custom_views.cropper.ImageCropperView
                android:id="@+id/image"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.0"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

            <android.support.v7.widget.Toolbar
                app:layout_collapseMode="pin"
                app:contentInsetLeft="0dp"
                app:contentInsetStart="0dp"
                android:layout_gravity="bottom"
                android:layout_width="match_parent"
                android:layout_height="@dimen/my_tab_height">
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                    <android.support.v7.widget.AppCompatImageView
                        android:layout_alignParentBottom="true"
                        style="@style/Icon"
                        android:src="@drawable/ic_rezoom"
                        android:id="@+id/rezoom"
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentStart="true" />

                    <android.support.v7.widget.AppCompatImageView
                        android:layout_alignParentBottom="true"
                        style="@style/Icon"
                        android:src="@drawable/ic_multiple_disabled"
                        android:id="@+id/multiSelect"
                        android:layout_alignParentEnd="true"
                        android:layout_alignParentRight="true" />
                </RelativeLayout>
            </android.support.v7.widget.Toolbar>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

The coordinator layout is working as expected but I want to change the behaviour of the AppBarLayout namely make it NOT collapse when scrolling down the recycler view but collapse when scrolling the AppBarLayout.

How can I do that?

Thanks in advance

Alexei Artsimovich
  • 1,074
  • 7
  • 15
  • 1
    Did you check this question? https://stackoverflow.com/questions/32557729/how-to-disable-collapsingtoolbars-collapse-when-scroll-has-not-content – Vikasdeep Singh Jul 07 '18 at 15:21

2 Answers2

1

According to android documentation for RecyclerView implementing the RecyclerView.OnScrollListener contains the method OnScroll(...) which contains delta_x and delta_y. Using the delta_y value can be used to detect change in the displacement of y direction. therefore,

if( dy > 0 ) { you're scrolling in the positive vertical direction }

else if( dy < 0 ) { you're scrolling in the negative vertical direction }

else { dy == 0, you're not scrolling or the visible item changed }

knowing this logic, if(dy<0) { do not collapse the AppBarLayout }, and you can make this happen by using setScrollFlags( int ) according to the android documentation for AppBarLayout.

EvOlaNdLuPiZ
  • 600
  • 1
  • 4
  • 21
  • following this way would ensure you have control of your application programmatically instead of setting a fixed value inside the xml file. – EvOlaNdLuPiZ Jul 07 '18 at 15:41
0

In your Activity class make the following changes

toolbar.setCollapsible(false);

In your XML file set

app:layout_scrollFlags="snap"

for CollapsingToolbarLayout

This change would avoid AppBarLayout to Collapse while scrolling down.

Hope this works.

Mayank Bhatnagar
  • 2,120
  • 1
  • 12
  • 20