【问题标题】:CollapsingToolbarLayout not collapse a custom viewCollapsingToolbarLayout 不折叠自定义视图
【发布时间】:2018-03-19 22:25:35
【问题描述】:

我有 AppBarLayout 和 ViewPager 的活动。 AppBarLayout 有 Toolbar、TabLayout 和 CollapsingToolbarLayout 和 LinearLayout。 ViewPager 显示包含 RecyclerView 的片段。我希望 CollapsingToolbarLayout 在滚动期间折叠,但我不能这样做。

我的活动

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

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:fitsSystemWindows="true"
            app:title="App" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="center"
            app:tabIndicatorHeight="0dp" />

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="@color/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    app:layout_collapseMode="parallax">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:padding="5dp"
                        android:text="Title" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:padding="5dp"
                        android:text="Subtitle" />
                </LinearLayout>

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

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

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorAccent"
        app:coinPages="@{viewModel.pages}"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

我实际拥有的

我正试图在滚动过程中“隐藏”标题

如果我为工具栏设置app:layout_scrollFlags="scroll|snap",则视差效果开始起作用,但它隐藏了工具栏(我不想要那个)并且仍然不会折叠 CollapsingToolbarLayout。看起来像这样

【问题讨论】:

    标签: android android-viewpager android-toolbar android-collapsingtoolbarlayout


    【解决方案1】:

    感谢site,我找到了解决方案。如果 AppBarLayout 不移动(如果我理解正确的话),CollapsingToolbarLayout 不能“折叠”视图。但我可以在 CoordinatorLayout 之外创建工具栏的静态部分。您可以使用app:elevation="0dp" 关闭第一个 AppBarLayout 的阴影。没有阴影,2 个工具栏看起来就像一个。利润!

    这是我的xml

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay"
            app:elevation="0dp">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:title="App" />
    
            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_collapseMode="pin"
                app:tabGravity="center"
                app:tabIndicatorHeight="0dp" />
        </android.support.design.widget.AppBarLayout>
    
        <android.support.design.widget.CoordinatorLayout
            android:id="@+id/main_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/appbar"
            tools:context=".Main2Activity">
    
            <android.support.design.widget.AppBarLayout
                android:id="@+id/appbar2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/AppTheme.AppBarOverlay">
    
                <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:layout_scrollFlags="scroll|enterAlways">
    
                    <android.support.constraint.ConstraintLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">
    
                        <TextView
                            android:id="@+id/textView"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="16dp"
                            android:gravity="center"
                            android:text="Title"
                            app:layout_constraintEnd_toEndOf="parent"
                            app:layout_constraintStart_toStartOf="parent"
                            app:layout_constraintTop_toTopOf="parent" />
    
                        <TextView
                            android:id="@+id/textView3"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginBottom="8dp"
                            android:layout_marginTop="8dp"
                            android:gravity="center"
                            android:text="Subtitle"
                            app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintEnd_toEndOf="parent"
                            app:layout_constraintStart_toStartOf="parent"
                            app:layout_constraintTop_toBottomOf="@+id/textView" />
    
                    </android.support.constraint.ConstraintLayout>
                </android.support.design.widget.CollapsingToolbarLayout>
    
            </android.support.design.widget.AppBarLayout>
    
            <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/colorAccent"
                app:coinPages="@{viewModel.pages}"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    
        </android.support.design.widget.CoordinatorLayout>
    </RelativeLayout>
    

    【讨论】:

      【解决方案2】:

      将 Fragment 中的 FrameLayout 更改为 NestedScrollView

      这是片段 xml 代码:

      <?xml version="1.0" encoding="utf-8"?>
      <androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
      
          <!-- TODO: Update blank fragment layout -->
          <TextView
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:text="myFragment" />
      
      </androidx.core.widget.NestedScrollView>
      

      不要忘记 app:layout_behavior="@string/appbar_scrolling_view_behavior"in ViewPager 这样的:

      <androidx.viewpager.widget.ViewPager
              android:id="@+id/container"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              app:layout_behavior="@string/appbar_scrolling_view_behavior"
              />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-09-02
        • 2016-03-06
        • 1970-01-01
        • 2015-12-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多