简体   繁体   English

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

[英]Recycler view Inside NestedScrollView with CoordinatorLayout

Below is the code snippet, can someone please help me? 以下是代码段,有人可以帮助我吗? My collapsing toolbar is not collapsing at all. 我正在折叠的工具栏根本没有折叠。 Intended behavior is : as I scroll up, the toolbar should collapse from 168dp to 56dp . 预期的行为是:当我向上滚动时,工具栏应该从168dp折叠到56dp But it is not collapsing at all. 但这根本没有崩溃。 Thanks in advance. 提前致谢。

<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...

EDIT: 编辑:

I played with your layout. 我玩过你的布局。 You have to use NestedScrollView in order to make your layout follow scroll behavior of CollapsingToolbarLayout . 您必须使用NestedScrollView才能使布局遵循CollapsingToolbarLayout滚动行为。 Following is the working xml code: 以下是有效的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>

But there is an issue with this approach. 但是这种方法存在问题。 If you put RecyclerView inside NestedScrollView when root parent is CoordinatorLayout . 如果您在根父级CoordinatorLayout时将RecyclerView放在NestedScrollView内。 Recycler's content won't be displayed, although all the adapter methods are called. 尽管调用了所有适配器方法,但不会显示回收者的内容。 Reason behind is the nesting of scroll layout inside scroll. 背后的原因是滚动布局在滚动内部的嵌套。 Most probably Recycler's layout is not rendered due to this reason. 由于这个原因,很可能没有渲染回收站的布局。 For that, work around has been followed from this post. 为此,围绕工作一直跟着从这个职位。

In your code, use WrappingLinearLayoutManager class as layout manager for recycler view. 在您的代码中,将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);

This should solve your problem. 这应该可以解决您的问题。 If it still doesn't work, I can upload it somewhere for you. 如果仍然无法使用,我可以将其上传到您的某个位置。

Just in case anybody else bumped into the same problem, I'm gonna post the solution to my issue. 以防万一其他人遇到相同的问题,我将发布解决方案。 The problem was with the support-library version, I was using 22.0.0. 问题出在support-library版本上,我正在使用22.0.0。 In this version, the SwipeRefreshLayout does not support CollapsibleToolbar behavior, it was a bug that got resolved in the 23.0 version. 在此版本中, SwipeRefreshLayout不支持CollapsibleToolbar行为,这是一个在23.0版本中得到解决的错误。 So, I updated my support - libaries to 23.0.0 and it got resolved! 所以,我更新了我的support - libaries图书馆到23.0.0,它得到解决了! yeay! yeay!

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

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