问题
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.
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>
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??
Thanks in advance.
回答1:
I made a version of Chris Bane's Cheesesquare demo but using fragments.
Video Demo
CheeseCategoriesFragment
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);
}
来源:https://stackoverflow.com/questions/35872566/collapsingtoolbarlayout-in-fragments