【问题标题】:Prevent CollapsingToolBarLayout from expanding programmatically防止 CollapsingToolBarLayout 以编程方式展开
【发布时间】:2016-08-29 07:43:51
【问题描述】:

我在我的主要活动中使用了 CollapsingToolBarLayout,但只希望它在某些片段中是可扩展的。为此,我一直在使用这些方法(在片段事务/分离之后)。这些成功地为工具栏设置了动画,但它仍然可以在调用 lockAppBarClosed 的片段中展开。

public void unlockAppBarOpen(boolean animate){
    appBarLayout.setExpanded(true, animate);
    appBarLayout.setEnabled(true);
    appBarLayout.setActivated(true);
}
public void lockAppBarClosed(boolean animate){
    appBarLayout.setExpanded(false, animate);
    appBarLayout.setEnabled(false);
    appBarLayout.setActivated(false);
}

setEnabledsetActivated 似乎什么也没做。

作为参考,这里是我的 app_bar_main.xml 文件:

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/app_bar_extended"
    android:fitsSystemWindows="true"
    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"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleTextAppearance="@android:color/transparent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">
        <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" />
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:clickable="true"
    android:background="?android:attr/selectableItemBackground"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
</FrameLayout>

我尝试通过 LayoutParams() 设置 AppBarLayout 的高度,但由于某种原因,这会阻止动画播放,即使 animate 设置为 true。将滚动标志设置为 none 或 always collapsed 什么都不做,我也不是在寻找滚动 snap 效果。

理想情况下,一个片段中不应该有应用栏的动画(就像正常一样),并且在片段之间和所需片段之间移动时应该有折叠/展开动画。

如果您有任何建议,我们将不胜感激。 谢谢。

【问题讨论】:

    标签: android animation android-fragments android-toolbar android-collapsingtoolbarlayout


    【解决方案1】:

    使用mAppBarLayout.setExpanded(true) 展开工具栏并使用mAppBarLayout.setExpanded(false) 以编程方式折叠工具栏

    如果你想阻止 CollapsingToolbarLayout 展开,那么你应该使用mAppBarLayout.setLayoutParams(params) 来改变 CollapsingToolbarLayout 高度编程。

    折叠:

    CoordinatorLayout.LayoutParams params =(CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
    params.height = 3*80; // COLLAPSED_HEIGHT
    
    mAppBarLayout.setLayoutParams(params);
    mAppBarLayout.setExpanded(false);
    

    展开:

    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
    params.height = 3*200; // EXPANDED_HEIGHT
    
    mAppBarLayout.setLayoutParams(params);
    mAppBarLayout.setExpanded(true);
    

    希望对你有帮助~

    【讨论】:

      【解决方案2】:

      建议您在主活动本身中使用操作栏或普通工具栏,而不是在 activity_main.xml 中使用 CollapsingToolBarLayout,而在您想要访问 CollapsingToolBarLayout 的那些特定片段中,将其添加到 xml 文件中片段如下:

                  <LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:app="http://schemas.android.com/apk/res-auto">
                  <android.support.design.widget.CoordinatorLayout
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                      android:id="@+id/specific_coordinate"
                  android:fitsSystemWindows="true">
      
                  <android.support.design.widget.AppBarLayout
                      android:id="@+id/app_bar_layout"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
      
                      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                      android:fitsSystemWindows="true">
      
                      <android.support.design.widget.CollapsingToolbarLayout
                          android:id="@+id/collapsing_toolbar"
                          android:layout_width="match_parent"
                          android:layout_height="match_parent"
                          app:layout_scrollFlags="scroll|exitUntilCollapsed"
                          app:contentScrim="?attr/colorPrimary"
      
                          app:expandedTitleMarginStart="48dp"
                          app:expandedTitleMarginEnd="64dp"
                          android:fitsSystemWindows="true">
      
                          <ImageView
                      android:id="@+id/pager"
                      android:layout_width="match_parent"
                      android:layout_height="250dp">
                  </ImageView>
      
                          <android.support.v7.widget.Toolbar
                              android:id="@+id/toolbar"
                              android:layout_width="match_parent"
                              android:layout_height="?attr/actionBarSize"
                              app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                              app:expandedTitleTextAppearance="@style/CollapsingToolbarLayoutExpandedTextStyle"
                              app:layout_collapseMode="pin" >
      
                          </android.support.v7.widget.Toolbar>
      
                      </android.support.design.widget.CollapsingToolbarLayout>
      
                  </android.support.design.widget.AppBarLayout>
      
      
                  <android.support.v4.widget.NestedScrollView
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:layout_gravity="fill_vertical"
                      android:layout_marginBottom="?attr/actionBarSize"
                      app:layout_behavior="@string/appbar_scrolling_view_behavior">
      
                      <include layout="@layout/specific_content" />
      
                  </android.support.v4.widget.NestedScrollView>
      
      
      
      
      
              </android.support.design.widget.CoordinatorLayout>
                  </LinearLayout>
      
          where the specific_content.xml is an xml file that carries the fragment contents.
      
          In the fragment code where you want the CollapsingToolBarLayout, disable the MainActivity actionbar/toolbar and you can see the CollapsingToolBarLayout there. Hope that will help. Happy coding.
      

      【讨论】:

        猜你喜欢
        • 2015-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-26
        • 2011-01-15
        • 2010-11-14
        相关资源
        最近更新 更多