【问题标题】:Collapsing Toolbar not working as desired折叠工具栏无法按预期工作
【发布时间】:2015-11-05 08:26:29
【问题描述】:

我正在处理折叠工具栏。我在工具栏下方添加了一个 RecyclerView。 我面临几个问题:

  1. 向上滚动 RecylerView 时,工具栏也必须向上滚动,但事实并非如此。要向上滚动工具栏,我们需要触摸工具栏并向上滚动。

  1. 当我的工具栏向下滚动时,我的最后一个元素被隐藏。

下面是xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent" android:fitsSystemWindows="true"
    tools:context=".ScrollingActivity">

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

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

            <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
                android:layout_height="?attr/actionBarSize" android:layout_width="match_parent"
                app:layout_collapseMode="pin" app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <include layout="@layout/content_scrolling"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        />

    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin" app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end" android:src="@android:drawable/ic_dialog_email" />

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

这是带有回收器视图的下一个滚动视图的 xml,包含在上述 xml 中:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.NestedScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:showIn="@layout/activity_scrolling"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        tools:context=".ScrollingActivity">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/my_recycler_view"
            android:scrollbars="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </android.support.v4.widget.NestedScrollView>

【问题讨论】:

  • 不要将 RecyclerView 放在 NestedScrollView 中。
  • 您也可以使用RelativeLayout 代替您的NestedScrollView。我试图在工具栏下方的项目中添加回收器查看器时发现了这一点
  • 那么我在哪里添加回收站视图?
  • 哦,@KaranMer 非常感谢。这么愚蠢的错误。我在哪里看到回收站视图在嵌套视图中。非常感谢你。我在这件愚蠢的事情上浪费了 3 天时间
  • 及其工作。但是当我向下滚动并且如果应用程序栏没有展开时,它会停在列表的第一项,然后我必须再次滚动它以展开它?如何解决这个问题

标签: android xml material-design android-appbarlayout


【解决方案1】:

试试这个:用下面的RecyclerView替换&lt;include&gt;并去掉NestedScrollView

<android.support.v7.widget.RecyclerView
    android:id="@+id/my_recycler_view"
    android:scrollbars="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_behaviour="@string/appbar_scrolling_view_behavior" />

您只需要app:layout_behaviour="@string/appbar_scrolling_view_behavior"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多