为第二个RecyclerView加载更多 - android

时间:2018-04-12 08:38:26

标签: android android-recyclerview

我的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:clickable="true">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/signsRecycler"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

</RelativeLayout>

以下布局是上面的RecyclerView项目,在这个布局中我有另一个RecyclerView。我怎样才能为第二个RecyclerView加载更多?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:id="@+id/cardVisibleLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        card_view:cardElevation="2dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:orientation="horizontal">

            <com.joanzapata.iconify.widget.IconTextView
                android:id="@+id/itxtArrow"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@null"
                android:gravity="center"
                android:text="@string/fa_chevron_down"
                android:textColor="@color/orangePeel" />

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_gravity="right"
                android:layout_weight="5"
                android:gravity="right"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/txtCategoryName"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="right"
                    android:layout_marginRight="15dp"
                    android:layout_weight="5"
                    android:gravity="center_vertical" />
            </RelativeLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/cardVisibleLayout">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rcyNested"
            android:layout_width="match_parent"
            android:layout_height="125dp" />
    </LinearLayout>

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

这可以帮到你:

How to load more data in RecyclerView when Scrolling on Android

一步一步:

  1. OnScrollListener添加到RecyclerView以检测您何时要加载更多内容(例如,如果您是最后一项,请加载更多内容)

  2. 为更多加载创建一个界面:

    public interface OnLoadMoreCallback { void onLoadMore(); }

  3. 实施界面。在onLoadMore()内,您可以调用例如休息api。为此,您可以使用AsyncTask

  4. 加载新数据后,您必须更新数据模型并通知适配器数据已添加。