簡體   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