繁体   English   中英

具有CoordinatorLayout的NestedScrollView内的回收者视图

[英]Recycler view Inside NestedScrollView with CoordinatorLayout

以下是代码段,有人可以帮助我吗? 我正在折叠的工具栏根本没有折叠。 预期的行为是:当我向上滚动时,工具栏应该从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...

编辑:

我玩过你的布局。 您必须使用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>

但是这种方法存在问题。 如果您在根父级CoordinatorLayout时将RecyclerView放在NestedScrollView内。 尽管调用了所有适配器方法,但不会显示回收者的内容。 背后的原因是滚动布局在滚动内部的嵌套。 由于这个原因,很可能没有渲染回收站的布局。 为此,围绕工作一直跟着从这个职位。

在您的代码中,将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);

这应该可以解决您的问题。 如果仍然无法使用,我可以将其上传到您的某个位置。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM