Tuesday, 6 August 2019

Collapsing/expanding Toolbar only on double click WITH a framelayout that is not covered by the toolbar

For my existing app I would like the following change:

  • By default the Toolbar/Appbar is visible. The contents of the FrameLayout starts below the toolbar. So, nothing of the Framelayout is covered by the Toolbar.
  • On a user action, e.g. by a double click, the Toolbar/Appbar should collapse. The Framelayout should stretch to the top of the page.
  • No collapsing of the Toolbar/Appbar is needed upon scrolling the content of the Framelayout.

How can I get this behaviour? Till so far I face the following issues: [1] the Toolbar/Appbar collapses when I scroll the FrameLayout and [2] part of the Framelayout is covered by the Toolbar/Appbar.

This is my code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:itemIconTint="#333"
                app:itemTextColor="#333"
                app:layout_collapseMode="pin"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
        </android.support.design.widget.AppBarLayout>

        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            app:layout_collapseMode="pin"/>

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

    <android.support.design.widget.NavigationView
        android:id="@+id/left_drawer"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/navigation_drawer_items"
        app:itemIconTint="#fcc"
        app:itemTextColor="#000" />

</android.support.v4.widget.DrawerLayout>

EDIT: (nearly solved the issue):

One step in the right direction is by making 1 change. [1] the framelayout is not covered by the toolbar, [2] the framelayout stretches to the top when the toolbar is collapsed.

The only issue is: how to prevent the toolbar to collapse when scrolling up in the framelayout?

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:itemIconTint="#333"
                app:itemTextColor="#333"
                app:layout_collapseMode="pin"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
        </android.support.design.widget.AppBarLayout>

        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
         <!-- see this line above. The string is defined below -->    

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

    <android.support.design.widget.NavigationView
        android:id="@+id/left_drawer"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/navigation_drawer_items"
        app:itemIconTint="#fcc"
        app:itemTextColor="#000" />

</android.support.v4.widget.DrawerLayout>

The layout string is:

<string name="appbar_scrolling_view_behavior" translatable="false">android.support.design.widget.AppBarLayout$ScrollingViewBehavior</string>



from Collapsing/expanding Toolbar only on double click WITH a framelayout that is not covered by the toolbar

No comments:

Post a Comment