Fragment with CollapsingToolbar showing Activity Toolbar

和自甴很熟 提交于 2019-12-14 02:43:24

问题


I have a CollapsingToolbarLayout in my fragment and is showing fine, the thing is that App Toolbar is also being showed. I try this solution: Can I use CollapsingToolbarLayout in a Fragment from Navigation Drawer but is not working for me...

This is my fragment AppBarLayout:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/layout_bar"
    android:layout_width="match_parent"
    android:layout_height="192dp">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapse_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
        app:contentScrim="#FFf3802b"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <ImageView
            android:id="@+id/brandLogo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:adjustViewBounds="true"
            android:contentDescription="@string/logo_icon"
            android:scaleType="centerCrop"
            android:src="@drawable/avatar_krono"
            app:layout_collapseMode="parallax"/>

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

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

</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
    android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    ...

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

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

My parent activity (activity_main.xml):

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/appToolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:popupTheme="@style/ThemeOverlay.AppCompat.Dark"/>

    <!-- Framelayout to display Fragments -->
    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

<!-- NavigationView to display slider menu -->
<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="280dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header"
    app:menu="@menu/drawer_view" />

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

I'm using in myFragmentActivity (just to try) this line to change the title of my APPToolBar and it really change:

((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Title");

Then, i try to hide my appToolbar by using this line but is not working, my appToolbar is always being showed:

((AppCompatActivity) getActivity()).getSupportActionBar().hide();

Any suggestions?


回答1:


Which type of layout are you using in the activity where you are inflating this fragment? Maybe a CoordinatorLayout can solve your problem.

http://developer.android.com/reference/android/support/design/widget/AppBarLayout.html

https://guides.codepath.com/android/Handling-Scrolls-with-CoordinatorLayout




回答2:


Your fragment replaces the inner layer, but not Acitvity toolbar. That all worked properly should be placed together with fragment container and toolbar. In your code:

<!-- Use this LinearLayout for container -->
<LinearLayout
    android:id="@+id/correct_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/appToolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:popupTheme="@style/ThemeOverlay.AppCompat.Dark"/>

    <!-- Framelayout to display Fragments -->
    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

Sure we can use another solution. We can create function in MainActivity and call in all fragments for closing actual Toolbar (Perform other code different from yours). And other....




回答3:


Change the color to transparent when you switch the your collapsing layout fragment.

Go through this you will get your solution:

https://stackoverflow.com/a/57771274/5328951



来源:https://stackoverflow.com/questions/33658981/fragment-with-collapsingtoolbar-showing-activity-toolbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!