我有一个包含片段的appcompatactivity,它控制许多片段的替换。我想根据要显示的片段显示不同的工具栏。
这是main_activity.xml的代码:

<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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.inthecheesefactory.lab.designlibrary.activity.MainActivity">

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

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

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="@dimen/expanded_toolbar_title_margin_start"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/image"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.7" />

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

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

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

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nest_scrollview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <FrameLayout android:id="@+id/container"
            android:layout_width="match_parent"
            android:clickable="true"
            android:layout_height="match_parent"
            android:layout_below="@+id/appBarLayout"/>

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


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

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:fitsSystemWindows="false"
    android:layout_gravity="start"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/drawer_menu_login"
    />

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

使用此代码,AppBarLayout将以ColorPrimary为背景折叠。我想在一些片段中禁用这个折叠,而在另一个片段中,我需要它在折叠工具栏布局的imageview中显示一个图像。
它在appbarlayout中有图像要折叠的片段中运行良好,但在没有任何图像要折叠的片段中,我想取消折叠以显示正常的工具栏而不进行任何扩展,这可能吗??
提前谢谢。

最佳答案

我制作了一个chris bane的cheesesquare演示,但是使用了片段。
version
奶酪分类碎片
基本上,在片段的onActivityCreated方法调用中,这些方法取决于您是否需要折叠。

public void disableCollapse() {
    imageView.setVisibility(View.GONE);
    tabLayout.setVisibility(View.VISIBLE);
    collapsingToolbar.setTitleEnabled(false);
}

public void enableCollapse() {
    imageView.setVisibility(View.VISIBLE);
    tabLayout.setVisibility(View.GONE);
    collapsingToolbar.setTitleEnabled(true);
}

10-08 07:50
查看更多