在Android中嵌套滚动内部的RecyclerView无限加载更多

8
我在NestedScrollView中嵌套了RecyclerView,我想要滑动NestedScrollView以显示加载项并将更多列表加载到RecyclerView中。
以下是我的XML代码。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="kh.com.iknow.endless.MainActivity">

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Filter"/>
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Sort"/>
            </LinearLayout>
            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:clipToPadding="false"
                android:scrollbars="vertical" />
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
</LinearLayout>

看一下这个链接,了解如何使用这个概念:https://github.com/codepath/android_guides/wiki/Endless-Scrolling-with-AdapterViews-and-RecyclerView - Kshitij Jain
如果NestedScrollView是一个滚动容器,你的代码将无法工作。 - wonsuc
2个回答

21

有两种方法可以实现:

1- 添加标题到RecyclerView

2- 使用NestedScrollView的setOnScrollChangeListener方法

我更倾向于第二种方法:

 NestedScrollView nestedSV = (NestedScrollView)      findViewById(R.id.nested_sync_scrollview);

 if (nestedSV != null) {

        nestedSV.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                String TAG = "nested_sync";
                if (scrollY > oldScrollY) {
                    Log.i(TAG, "Scroll DOWN");
                }
                if (scrollY < oldScrollY) {
                    Log.i(TAG, "Scroll UP");
                }

                if (scrollY == 0) {
                    Log.i(TAG, "TOP SCROLL");
                }

                if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
                    Log.i(TAG, "BOTTOM SCROLL");
                    if (!isRecyclerViewWaitingtoLaadData) //check for scroll down
                    {

                        if (!loadedAllItems) {
                            showUnSentData();
                        }
                    }
                }
            }
        });
    }

1
很棒的解决方案。 - Ahamadullah Saikat
如果@angkor-empire的回答解决了您的问题,请将其视为正确答案。 - iDeveloper
你需要接受他的答案,它是有效的,谢谢@iDeveloper。 - Antony Sunggaki

0
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/off_white_bg"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="2dp"
        android:fitsSystemWindows="true">

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/nestedScrollViewId"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:fillViewport="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:focusableInTouchMode="true"
                android:orientation="vertical"
                app:layout_scrollFlags="scroll|enterAlways">

                <LinearLayout
                    android:id="@+id/permiumLAyout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_below="@+id/divider"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="10dp"
                    android:focusableInTouchMode="true"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:visibility="visible">
                    <!--  List view -->

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@color/btn_br_color"
                        android:padding="3dp">

                        <TextView
                            android:id="@+id/primiumGameNameText"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="fill"
                            android:layout_marginLeft="15dp"
                            android:gravity="center"
                            android:maxLines="4"
                            android:text="Primium Games"
                            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                            android:textColor="@color/row_text_color"
                            android:textSize="18sp"
                            android:textStyle="bold" />

                    </LinearLayout>

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/recycler_view_primiumGameList"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:isScrollContainer="false"
                        android:nestedScrollingEnabled="false"
                        android:paddingBottom="3dp"
                        android:paddingLeft="5dp"
                        android:paddingRight="5dp"
                        android:paddingTop="3dp" />

                </LinearLayout>

            </LinearLayout>

        </android.support.v4.widget.NestedScrollView>

    </FrameLayout>

</LinearLayout>

我注意到一个问题,将我的RecyclerView放在NestedScrollView中。我发现滚动RecyclerView的内容会变得缓慢。在滚动方面出现了问题。另外一个主要问题是,在NestedScrollView中加载更多数据时遇到了问题。 解决方法:使用以下类似的load more事件

mRecAdapter = new RecyclerAdapter(recylerView,
        primiumgameDetailsArrayList, getActivity(), cost);
recylerView.setAdapter(mRecAdapter);

//set load more listener for the RecyclerView adapter
mRecAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
    @Override
    public void onLoadMore () {
    **mRecyclerView.setNestedScrollingEnabled(false);**
        if (reqCountStatus.equalsIgnoreCase("true")) {
            primiumgameDetailsArrayList.add(null);
            mRecAdapter.notifyItemInserted(primiumgameDetailsArrayList.size() - 1);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {

                    primiumgameDetailsArrayList.remove(primiumgameDetailsArrayList.size() - 1);
                    mRecAdapter.notifyItemRemoved(primiumgameDetailsArrayList.size());
                    int index = primiumgameDetailsArrayList.size();
                    primiumGameloadData(mRecyclerView, index);
                }
            }, 5000);
        } else {
            mRecyclerView.setNestedScrollingEnabled(false);
            Toast.makeText(getActivity().getApplicationContext(),
                    "Loading data completed", Toast.LENGTH_SHORT).show();
        }
    }
});

注意:我使用了以下方法禁用了RecyclerView的滚动功能:mRecyclerView.setNestedScrollingEnabled(false);,并在需要加载更多数据时启用:mRecyclerView.setNestedScrollingEnabled(true)

最好将代码和解释分开。 - wonsuc
你好,这里的 setOnLoadMoreListener 是什么?你能发一下你的代码吗? - Munir
这里的setOnLoadMoreListener是什么?你能发一下你的代码吗? - famfamfam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接