简体   繁体   中英

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 . 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 . Following is the working xml code:

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

    //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. In this version, the SwipeRefreshLayout does not support CollapsibleToolbar behavior, it was a bug that got resolved in the 23.0 version. So, I updated my support - libaries to 23.0.0 and it got resolved! yeay!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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