【问题标题】:CoordinatorLayout doesn't work like I want it toCoordinatorLayout 不像我想要的那样工作
【发布时间】:2015-09-30 11:38:14
【问题描述】:

我的活动中有此布局代码。在ViewPager 我有包含 RecyclerView 的片段。当我滚动到列表顶部时,我必须再次滚动它以使 CoordinatorLayout 展开。我怎样才能在一个无缝滚动中做到这一点:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:fitsSystemWindows="true"
    android:background="?colorPrimary">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:expandedTitleTextAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse"
        app:expandedTitleMarginStart="20dp"
        app:expandedTitleMarginBottom="10dp"
        app:contentScrim="?attr/colorPrimary"
        android:fitsSystemWindows="true"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <ImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_collapseMode="parallax"
            android:scaleType="centerCrop"
            android:transitionName="obrazek"
            android:src="@drawable/placer" />

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/gradient" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:contentInsetStart="40dp"
            app:layout_scrollFlags="scroll|enterAlways"
            app:layout_collapseMode="pin"
             />

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

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

<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" >

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="horizontal"
        android:background="?attr/colorPrimary"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</LinearLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    app:fabSize="normal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    app:elevation="6dp"
    app:layout_anchor="@id/main_content"
    app:layout_anchorGravity ="bottom|right|end"
    android:src="@drawable/ic_directions_run_white_48dp" />

【问题讨论】:

  • ViewPager 中的RecyclerView 不是您的CoordinatorLayout 的一部分,因此它无法协调滚动动作。
  • 您必须从 developer.android.com/reference/android/support/design/widget/… 创建自定义行为才能做到这一点。
  • 您为什么认为它与CoordinatorLayout 有任何关系?显然是 CollapsableToolbarLayout/AppBarLayout 拒绝扩展。自定义行为可能会为您解决这个问题,但我怀疑有一个更简单的解决方案。

标签: android android-recyclerview android-coordinatorlayout


【解决方案1】:

您不需要为您的 TabLayoutViewPager 设置 app:layout_behavior 。将app:layout_behavior 设置为仅LinearLayout 就足够了。

【讨论】:

  • 它的行为仍然相同。我认为您的回答更多的是优化而不是解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多