【问题标题】:Recycler view Inside NestedScrollView with CoordinatorLayout带有 CoordinatorLayout 的 NestedScrollView 内的回收器视图
【发布时间】:2016-06-13 04:53:58
【问题描述】:

下面是代码sn-p,有人可以帮我吗?我的折叠工具栏根本没有折叠。预期的行为是:当我向上滚动时,工具栏应该从168dp 折叠到56dp。但它根本没有崩溃。 提前致谢。

<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.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/one_primaryColor"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="168dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            app:layout_collapseMode="pin">

            <ImageView
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:contentDescription="@string/app_name"

            app:layout_collapseMode="parallax"
            android:src="@drawable/logo" />

        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
    <android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipe_refresh_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.v7.widget.RecyclerView...

【问题讨论】:

  • AppBarLayout 下面是什么?
  • @AntonTarasov 请立即检查代码。
  • @AntonTarasov 这两者有什么区别?我错过了什么?它是否需要我没有设置的特定主题/样式/属性?

标签: android material-design android-toolbar android-collapsingtoolbarlayout


【解决方案1】:

编辑:

我玩过你的布局。您必须使用NestedScrollView 以使您的布局遵循CollapsingToolbarLayout 的滚动行为。以下是工作 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.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/one_primaryColor">


        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="168dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            android:fitsSystemWindows="true">

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


                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:contentDescription="@string/app_name"
                    android:src="@drawable/logo" />

            </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:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                />

        </RelativeLayout>

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

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

但这种方法存在问题。如果您将RecyclerView 放在NestedScrollView 中,而root parentCoordinatorLayout。尽管调用了所有适配器方法,但不会显示 Recycler 的内容。背后的原因是滚动布局嵌套在滚动中。由于这个原因,很可能没有呈现 Recycler 的布局。为此,已从this 发布解决方法。

在您的代码中,使用 WrappingLinearLayoutManager 类作为回收器视图的布局管理器。

    //Your custom adapter
    Adapter adapter = new Adapter(cursor);
    adapter.setHasStableIds(true);
    mRecyclerView.setAdapter(adapter);
    mRecyclerView.setNestedScrollingEnabled(false);

    int columnCount = getResources().getInteger(R.integer.list_column_count);
    WrappingLinearLayoutManager wrappingLinearLayoutManager =
            new WrappingLinearLayoutManager(columnCount, LinearLayout.VERTICAL);
    mRecyclerView.setLayoutManager(wrappingLinearLayoutManager);

这应该可以解决您的问题。如果还是不行,我可以上传给你。

【讨论】:

【解决方案2】:

以防万一其他人遇到同样的问题,我将发布我的问题的解决方案。问题出在support-library 版本上,我使用的是 22.0.0。在这个版本中,SwipeRefreshLayout 不支持CollapsibleToolbar 行为,这是一个在 23.0 版本中得到解决的错误。所以,我将我的support - libaries 更新为 23.0.0 并解决了!耶!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-18
    • 1970-01-01
    • 2017-01-16
    • 1970-01-01
    相关资源
    最近更新 更多