2

My collapintoolbar is not working very well..

  1. The collapsingToolbarLayout.setTitle only work after the collapse is finished. Im try to set this when i change the fragment.

  2. When i change the fragment, the toolbar remains in the same state: example

I want to if the toolbar is collapsed and I ask to switch fragment, toolbar goes to expand state again.

Here my xml from MainActivity:

<android.support.v4.widget.DrawerLayout android:id="@+id/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:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:fitsSystemWindows="true"
                                    tools:context=".MainActivity">

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="160dp"
        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:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleMarginEnd="64dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitXY"
                android:src="@drawable/appbar_image"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.7"
                android:alpha="0.7"/>

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

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

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

    </FrameLayout>

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


<android.support.design.widget.NavigationView
    android:id="@+id/navigationView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:clickable="true"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header"
    app:menu="@menu/drawer_view"/>

And here my code to change the title:

//On fragment
public void onResume(){
    super.onResume();

    // Set title bar
    ((MainActivity) getActivity())
            .setActionBarTitle(getString(R.string.app_name));

}

//on mainactivity
public void setActionBarTitle(String title) {
    collapsingToolbarLayout.setTitle(title);
}
kittykittybangbang
  • 2,380
  • 4
  • 16
  • 27
Spotik
  • 105
  • 1
  • 2
  • 10

1 Answers1

1

This should not happen anymore in the last version 22.2.1

Here was a workaround for it https://stackoverflow.com/a/31309381/1695078

And here is the ticket https://code.google.com/p/android/issues/detail?id=175808#c8

Community
  • 1
  • 1
Marcel
  • 2,094
  • 3
  • 23
  • 37