0

I want AppBarLayout to remain in collapsed state for some of the fragments and remained open for other..Basically, i am looking for a function call which will make appbarlayout collapse/uncollapse programatically.... I have tried almost all methods listed on this stack over flow page, but none is working for me. Can someone please help.

Similar stack over flow page , tried most of things here but notworking.. Need to disable expand on CollapsingToolbarLayout for certain fragments

thanks for your time

Layout file 


    <?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"
        android:fitsSystemWindows="true"
        tools:context="app.com.navact.MainActivity">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/appBarLayout"
            android:theme="@style/AppTheme.AppBarOverlay">

            <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"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:expandedTitleMarginStart="48dp"
                app:expandedTitleMarginEnd="64dp">

            <ImageView
                android:id="@+id/backdrop"
                android:layout_width="match_parent"
                android:layout_height="256dp"
                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"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />
            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>


        <FrameLayout
            android:id="@+id/fragment_container"
            android:orientation="vertical"
            android:paddingTop="0dip"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
         </FrameLayout>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            app:srcCompat="@android:drawable/ic_dialog_email" />

    </android.support.design.widget.CoordinatorLayout>
Community
  • 1
  • 1
user7327850
  • 229
  • 5
  • 15

1 Answers1

4

You can call appBarLayout.setExpanded(true) to expand the app bar layout and appBarLayout.setExpanded(false) to collapse it.

To lock the app bar layout into the expanded or collapsed state, call setNestedScrollingEnabled(false) on the scrolling view that moves the app bar. That view will somewhere in the view hierarchy of the fragment in your fragment container.

kris larson
  • 30,387
  • 5
  • 62
  • 74
  • This solutions works, but if you would tap on collapsed toolbar and swipe down, it would do it, since this solution locks only nested scroll and just collpase the toolbar. I am facing this problem at the moment and one more another problem which I posted [here](https://stackoverflow.com/questions/65304976/access-view-on-content-main-xml-from-a-fragment?noredirect=1#comment115502514_65304976). If you have any ideas to help me with my problem, I would be very thankfull to you. – Mark Delphi Dec 25 '20 at 00:09