NestedScrollView嵌套RecyclerView導(dǎo)致RecyclerView復(fù)用失效的解決方法

??在使用CoordinatorLayout布局時,想在NestedScrollView里嵌套RecyclerView來加載網(wǎng)絡(luò)數(shù)據(jù)來形成頂部AppBarLayout跟隨NestedScrollView移動的效果,效果如下所示。

1.gif

??布局代碼如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:fitsSystemWindows="true"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/Srl_Doc"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:srlPrimaryColor="@color/colorNull"
        app:srlAccentColor="@color/colorPink"
        app:srlEnablePreviewInEditMode="true"
        app:srlEnableHeaderTranslationContent="false"
        >
        <android.support.design.widget.CoordinatorLayout
            android:fitsSystemWindows="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/Ab_Layout"
            android:fitsSystemWindows="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collpasing_toolbar_layout"
                app:contentScrim="#8BC34A"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:collapsedTitleGravity="center"
                app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
                android:fitsSystemWindows="true"
                >

                <include
                    android:scaleType="fitXY"
                    app:layout_collapseMode="parallax"
                    android:contentDescription="TODO"
                    android:fitsSystemWindows="true"
                    android:id="@+id/doc_list_top_bar"
                    layout="@layout/doc_list_top_bar" />

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    app:contentInsetLeft="0dp"
                    app:contentInsetStart="0dp"
                    android:theme="@style/MyToolbarTheme"
                    app:contentInsetStartWithNavigation="0dp"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:minHeight="?attr/actionBarSize"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/OverFlowMenyTheme"
                    >
                    <android.support.v7.widget.ActionMenuView
                        android:gravity="center"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"/>
                </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"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            >
                <android.support.v7.widget.RecyclerView
                    app:layout_constraintTop_toBottomOf="@+id/Ab_Layout"
                    app:layout_constraintBottom_toBottomOf="parent"
                    android:id="@+id/Rv_Doc"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>
        </android.support.v4.widget.NestedScrollView>
        </android.support.design.widget.CoordinatorLayout>
    </com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>

??這樣做就有一個問題,NestedScrollView嵌套RecyclerView會導(dǎo)致RecyclerView每次加載新數(shù)據(jù)時,都會從頭開始一次性調(diào)用全部onCreateViewHolder和onBindViewHolder來綁定數(shù)據(jù)。

2.png

??數(shù)據(jù)小的時候沒什么事,數(shù)據(jù)多的時候就會OOM了。。。

??經(jīng)過三天各種焦頭爛額的查閱請教測試,終于解決了這個問題。

??下面是新布局。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:fitsSystemWindows="true"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/Srl_Doc"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:srlPrimaryColor="@color/colorNull"
        app:srlAccentColor="@color/colorPink"
        app:srlEnablePreviewInEditMode="true"
        app:srlEnableHeaderTranslationContent="false"
        >
        <com.scwang.smartrefresh.header.BezierCircleHeader
            android:background="@color/colorNull"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <com.scwang.smartrefresh.layout.footer.BallPulseFooter
            app:srlAnimatingColor="@color/colorPink"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>



        <android.support.design.widget.CoordinatorLayout
            android:fitsSystemWindows="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/Ab_Layout"
            android:fitsSystemWindows="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collpasing_toolbar_layout"
                app:contentScrim="#8BC34A"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:collapsedTitleGravity="center"
                app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
                android:fitsSystemWindows="true"
                >

                <include
                    app:layout_collapseMode="parallax"
                    android:contentDescription="TODO"
                    android:fitsSystemWindows="true"
                    android:id="@+id/doc_list_top_bar"
                    android:adjustViewBounds="true"
                    layout="@layout/doc_list_top_bar" />


                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    app:contentInsetLeft="0dp"
                    app:contentInsetStart="0dp"
                    android:theme="@style/MyToolbarTheme"
                    app:contentInsetStartWithNavigation="0dp"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:minHeight="?attr/actionBarSize"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/OverFlowMenyTheme"
                    >

                    <android.support.v7.widget.ActionMenuView
                        android:id="@+id/amv"
                        android:gravity="center"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"/>
                </android.support.v7.widget.Toolbar>

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

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

            <FrameLayout
                android:id="@+id/Fl_Layout"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            <android.support.v4.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            </android.support.v4.widget.NestedScrollView>

            </FrameLayout>

        </android.support.design.widget.CoordinatorLayout>
    </com.scwang.smartrefresh.layout.SmartRefreshLayout>
</FrameLayout>

??改動不大,就是把NestedScrollView嵌套RecyclerView換成了FrameLayout嵌套NestedScrollView,然后把FrameLayout寫成fragment ,RecyclerView在fragment里面初始化和加載。

<?xml version="1.0" encoding="utf-8"?>
<!--RecyclerView單獨(dú)抽出來放一個xml文件里面 ,我這里是doc_fragment.xml-->
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <android.support.v7.widget.RecyclerView
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    android:id="@+id/Rv_Doc"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>
//RecyclerView在fragment里面初始化和刷新布局
public class Doc_Fragment extends BaseFragment implements DetailPageDocList.OnTriggerListener{
    @Bind(R.id.Rv_Doc)
    RecyclerView RvDoc;

    private DetailPageDocListAdapter adapter;

    private List<BilibiliAlbum.DataBean.ItemsBean> items = new ArrayList<>();

    @Override
    public View initView() {
        View view = View.inflate(mContext, R.layout.doc_fragment, null);
        System.out.println("Fragment初始化布局成功");
        return view;

    }

    @Override
    public void initData() {
        super.initData();
        System.out.println("Fragment初始化數(shù)據(jù)成功");
    }


    public void initRvDoc(List<BilibiliAlbum.DataBean.ItemsBean> items){
        this.items.clear();
        this.items.addAll(items);
        adapter = new DetailPageDocListAdapter(mContext,this.items);
        //記得寫上manager
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        RvDoc.setLayoutManager(manager);
        RvDoc.setAdapter(adapter);
    }


    public void notifyRvDoc(List<BilibiliAlbum.DataBean.ItemsBean> items){
        this.items.addAll(items);
        System.out.println("**********************DttailPageDocList的items是 " + this.items.size());
        adapter.setDataBean(this.items);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    @Override
    public void onTrigger(List<BilibiliAlbum.DataBean.ItemsBean> items, int i) {
        if (i == 0){
            //刷新
            initRvDoc(items);
        }
        if (i == 1){
            //加載
            notifyRvDoc(items);
        }
    }
}

??在Activity里面把布局文件里的FrameLayout初始化成Fragment,通過接口回調(diào)傳遞數(shù)據(jù)給Fragment。

    private void setInitFragment() {
        Doc_Fragment baseFragment = new Doc_Fragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.Fl_Layout, baseFragment);
        transaction.commit();
    }
3.gif

??可以看到RecyclerView的復(fù)用機(jī)制能實(shí)現(xiàn)了。

??最后特別感謝android開發(fā)技術(shù)群里的前輩們給予的幫助~~~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容