片段中的CollapsingToolbarLayout [英] CollapsingToolbarLayout in fragments

查看:71
本文介绍了片段中的CollapsingToolbarLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含片段的AppCompatActivity,它控制许多片段的替换.我想显示不同的工具栏取决于要显示的片段.

I have an AppCompatActivity that contain fragments and it controls the replacing of many fragments. I want to show differents toolbar depending of fragment to show.

这是main_activity.xml的代码:

This is the code for 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放在背景上.我想在某些片段中禁用此折叠,在其他片段中,我需要它以在CollapsingToolBarLayout的ImageView中显示图像. 在AppBarLayout中具有要折叠的图像的片段中效果很好,但是在没有任何图像可以折叠的片段中,我想取消折叠以显示没有任何展开的普通工具栏,这有可能吗?

With this code the AppBarLayout is collapsed with the colorPrimary on background. I want to disable this collapsed in some fragments and in another fragments I need it to show an image in the ImageView of CollapsingToolBarLayout. It works fine in fragments that have an image to collapse in AppBarLayout but in the fragments that not have any image to collapse I want to cancel the collapse to show a normal toolbar without any expand, is it possible??

谢谢.

推荐答案

我制作了Chris的版本 Bane的Cheesesquare演示,但使用了片段.

I made a version of Chris Bane's Cheesesquare demo but using fragments.

视频演示

CheeseCategoriesFragment

CheeseCategoriesFragment

基本上,在片段的onActivityCreated方法调用中,这些方法取决于您是否要折叠.

Basically, in the onActivityCreated methods of fragments call, these methods depending on whether you want the collapse or not.

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);
}

这篇关于片段中的CollapsingToolbarLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆