如何在 RecyclerView 滚动时实现分页 [英] How to implement pagination in RecyclerView on scroll

查看:40
本文介绍了如何在 RecyclerView 滚动时实现分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Recyclerview 带有自己的滚动侦听器,它具有以下方法:

void onScrollStateChanged(RecyclerView recyclerView, int newState)

RecyclerView 的滚动状态改变时调用的回调方法.

void onScrolled(RecyclerView recyclerView, int dx, int dy)

滚动 RecyclerView 时调用的回调方法.

有没有办法在滚动到列表末尾时触发加载器加载更多数据?

<块引用>

我是这样实现的:

@Override公共无效 onBindViewHolder(ViewHolder viewHolder, int i) {GenerItemgenerItem=generItems.get(i);Log.d("TAG","位置"+i);if(i==generItems.size()-1)((GeneSearchActivity)mContext).onScroll(i);viewHolder.bindValues(generItem);}

这里onScroll()在Activity中,会触发loader加载更多数据.请建议最好的方法是什么.

解决方案

在滚动结束时进行下一个调用

基本上有 3 个步骤.

  1. 滚动列表时通知
  2. 进行 REST 调用(用于下一页)
  3. 将结果添加到旧列表中 + 通知数据集更改

回调

但首先,我们需要一个回调函数作为 RecyclerView.Adapter 和 Activity 之间的桥梁

公共接口PaginationCallBack{公共无效 oadNextPage();}

<块引用>

在你的活动中实现这个回调

class YourActivity 扩展 AppCompatActivity 实现 PaginationCallBack{int pageNum = 1;@覆盖public void loadNextPage(){//你的实现}}

<块引用>

在 RecyclerView.Adapter 中初始化回调

class YourAdapter 扩展 RecyclerView.Adapter{私人 PaginationCallBack paginationCallBack;公共你的适配器(PaginationCallBack paginationCallBack){this.paginationCallBack = paginationCallBack;}}

第 1 步onBindViewHolder 方法中添加条件并通过回调通知.

@Overridepublic void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewholder, int position) {if(position+1==images.size()){paginationCallBack.loadNextPage();//打回来}}

第 2 步:调用下一页

getImages(++pageNum)//你的 REST 方法是哪个页码

@Overridepublic void loadNextPage(){getImages(++pageNumber)//REST 调用下一页}

第 3 步将结果添加到旧列表中并通知 datasetChanged

public void getImages(int pageNum){列表<图像>newResults =//RESTCALLimageList.addAll(newResults);adapter.updateDataSet(imageList)}

updateDataSet(imageList) 方法在哪里?

在 RecyclerView.Adapter 里面写这个方法

 public void updateDataSet(List newImages){if(newImages!=null){图像 = 新图像;}notifyDataSetChanged();}

完整代码

Recyclerview comes with its own scroll listener which has the following methods :

void onScrollStateChanged(RecyclerView recyclerView, int newState)

Callback method to be invoked when RecyclerView's scroll state changes.

void onScrolled(RecyclerView recyclerView, int dx, int dy)

Callback method to be invoked when the RecyclerView has been scrolled.

Is there any way to trigger loader to load more data when scroll reaches end of the list?

I have implemented this way:

@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
    GenerItem generItem=generItems.get(i);
    Log.d("TAG","position "+i);
    if(i==generItems.size()-1)
    ((GenerSearchActivity)mContext).onScroll(i);
    viewHolder.bindValues(generItem);
}

Here onScroll() in Activity, will trigger the loader to load more data. What is the best way please suggest.

解决方案

Make Next Call at the end of scroll

There are essentially 3 steps.

  1. Notify when the list is scrolled
  2. Make REST call (for NEXT page)
  3. Add the result in the old list + Notify DataSet change

CALLBACK

But first, we need a Callback which would work as a bridge between RecyclerView.Adapter and Activity

public interface PaginationCallBack{
     public void oadNextPage();
}

Implement this callback in Your Activity

class YourActivity extends AppCompatActivity implements PaginationCallBack{

     int pageNum = 1;

     @Override
     public void loadNextPage(){
           // Your implementation
     }
}

Initialize Callback in RecyclerView.Adapter

class YourAdapter extends RecyclerView.Adapter{

     private PaginationCallBack paginationCallBack;

     public YourAdapter(PaginationCallBack paginationCallBack) {
        this.paginationCallBack = paginationCallBack;
     }

}

STEP 1 Add a condition in onBindViewHolder method and notify with a Callback.

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewholder, int position) {

    if(position+1==images.size()){
        paginationCallBack.loadNextPage();  // Callback
    }

}

Step 2: Call NEXT Page

getImages(++pageNum) // YOUR REST method which page number

@Override
public void loadNextPage(){
    getImages(++pageNumber) // REST call with next Page 
}

Step 3 Add the result in old list and notify datasetChanged

public void getImages(int pageNum){

      List<Images> newResults = //RESTCALL
      imageList.addAll(newResults);
      adapter.updateDataSet(imageList)

}

Where is updateDataSet(imageList) method?

Write this method inside RecyclerView.Adapter

 public void updateDataSet(List<GalleryMedia> newImages){

    if(newImages!=null){
        images = newImages;
    }

    notifyDataSetChanged();
}

Full Code

RecyclerView Pagination

Result:

这篇关于如何在 RecyclerView 滚动时实现分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆