【问题标题】:Android parallax effect CollapsingToolbarLayout with ViewGroup Layout is not working带有 ViewGroup 布局的 Android 视差效果 CollapsingToolbarLayout 不起作用
【发布时间】:2016-05-31 18:20:35
【问题描述】:

我正在开发一个 Android 应用程序。在我的应用程序中,我使用 CollapsingToolbarLayout 来实现视差效果。在我使用 AppbarLayout、CollapsingToolbarLayout 和 ViewPager 之前,我工作得很好。但是这一次,我用 RelativeLayout 替换了内容的视图持有者。因此,当我从内容向上滚动时,工具栏不会折叠。我的意思是当我从下面屏幕截图中圈出的位置向上滚动时。

但是,如果我从工具栏区域向上滚动,它就可以工作。我指的是屏幕截图中的区域。

这是我的 XML 布局

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_light"
    android:fitsSystemWindows="true"
    >

    <android.support.design.widget.AppBarLayout
        android:id="@+id/ai_app_bar"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:fitsSystemWindows="true"
        >

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/main.collapsing"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleMarginEnd="64dp"
            >

           <RelativeLayout
               android:background="@drawable/item"
               app:layout_collapseMode="parallax"
               android:layout_width="match_parent"
               android:layout_height="match_parent">
              <View
                  android:background="@color/lightGray"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"></View>
           </RelativeLayout>

            <android.support.v7.widget.Toolbar
                android:id="@+id/ai_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_collapseMode="pin"
                />
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>


    <RelativeLayout
        app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:text="This is testing"
            android:textColor="@color/textColorPrimary"
            android:layout_centerInParent="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/ai_review_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:src="@android:drawable/btn_star"
        android:layout_margin="10dp"
        android:visibility="gone"
        app:layout_anchor="@id/ai_app_bar"
        app:layout_anchorGravity="bottom|right|end"
        />
</android.support.design.widget.CoordinatorLayout>

当我从相对布局区域向上滚动时,为什么工具栏没有折叠?如果我从RelativeLayout 区域向上滚动,如何解决该问题使工具栏折叠?

【问题讨论】:

    标签: android android-layout android-appbarlayout android-collapsingtoolbarlayout


    【解决方案1】:

    您应该在AppBarLayout 之外使用RecyclerView 而不是RelativeLayout,并将此视图标签添加到app:layout_behavior="@string/appbar_scrolling_view_behavior"

    string/appbar_scrolling_view_behavior 映射到 AppBarLayout.ScrollingViewBehavior,用于通知 当此特定视图上发生滚动事件时的 AppBarLayout。

    从这里Handling Scrolls with CoordinatorLayout

    编辑

    此外,CollapsingToolbar 需要 RecyclerView 或 NestedListView 才能工作。

    【讨论】:

    • 我需要将它与RelativeLayout一起使用,因为内容不在列表中。
    【解决方案2】:

    在相对布局中,在相对布局中关闭每个标签后将其关闭

    <android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:fillViewport="true"
        android:fitsSystemWindows="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_gravity="fill_vertical">
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-30
      相关资源
      最近更新 更多