如何在scrolllerView上滚动实现分页

时间:2015-10-05 18:13:11

标签: android android-recyclerview recycler-adapter

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

void    onScrollStateChanged(RecyclerView recyclerView, int newState)

当RecyclerView的滚动状态发生变化时要调用的回调方法。

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

滚动RecyclerView时要调用的回调方法。

当滚动到达列表末尾时,是否有任何方法可以触发加载程序加载更多数据?

我已经实现了这种方式:

@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);
}

这里onScroll()in activity将触发加载器加载更多数据。请建议最好的方法是什么。

2 个答案:

答案 0 :(得分:1)

在滚动结束时进行下一个呼叫

enter image description here

基本上有3个步骤。

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

回叫

但是首先,我们需要一个Callback,它将作为RecyclerView.Adapter和Activity之间的桥梁

public interface PaginationCallBack{
     public void oadNextPage();
}

在您的活动中实施此回调

class YourActivity extends AppCompatActivity implements PaginationCallBack{

     int pageNum = 1;

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

在RecyclerView.Adapter中初始化回调

class YourAdapter extends RecyclerView.Adapter{

     private PaginationCallBack paginationCallBack;

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

}

步骤1 onBindViewHolder方法中添加条件并通过回调进行通知。

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

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

}

第2步:呼叫下一页

getImages(++ pageNum)//您的REST方法的页码

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

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

public void getImages(int pageNum){

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

}

updateDataSet(imageList)方法在哪里?

将此方法写入RecyclerView.Adapter

 public void updateDataSet(List<GalleryMedia> newImages){

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

    notifyDataSetChanged();
}

完整代码

RecyclerView Pagination

结果:

enter image description here

答案 1 :(得分:0)

Here's a scroll listener which implements load more and quick return pattern:

public abstract class HidingScrollListener extends RecyclerView.OnScrollListener {

    private static final float  HIDE_THRESHOLD   = 10;
    private static final float  SHOW_THRESHOLD   = 70;

    private int                 mToolbarOffset   = 0;
    private boolean             mControlsVisible = true;
    private int                 mToolbarHeight;
    private int                 mTotalScrolledDistance;
    private int                 previousTotal    = 0;
    private boolean             loading          = true;
    private int                 visibleThreshold = 4;
    int                         firstVisibleItem, visibleItemCount, totalItemCount;
    private LinearLayoutManager layoutManager;

    public HidingScrollListener(Context context, LinearLayoutManager layoutManager) {
        mToolbarHeight = Tools.getFooterHeight(context);
        this.layoutManager = layoutManager;
    }

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);

        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
            if (mTotalScrolledDistance < mToolbarHeight) {
                setVisible();
            }
            else {
                if (mControlsVisible) {
                    if (mToolbarOffset > HIDE_THRESHOLD) {
                        setInvisible();
                    }
                    else {
                        setVisible();
                    }
                }
                else {
                    if ((mToolbarHeight - mToolbarOffset) > SHOW_THRESHOLD) {
                        setVisible();
                    }
                    else {
                        setInvisible();
                    }
                }
            }
        }

    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

        clipToolbarOffset();
        onMoved(mToolbarOffset);

        if ((mToolbarOffset < mToolbarHeight && dy > 0) || (mToolbarOffset > 0 && dy < 0)) {
            mToolbarOffset += dy;
        }
        if (mTotalScrolledDistance < 0) {
            mTotalScrolledDistance = 0;
        }
        else {
            mTotalScrolledDistance += dy;
        }
        // for load more
        visibleItemCount = recyclerView.getChildCount();
        totalItemCount = layoutManager.getItemCount();
        firstVisibleItem = layoutManager.findFirstVisibleItemPosition();

        if (loading) {
            if (totalItemCount > previousTotal) {
                loading = false;
                previousTotal = totalItemCount;
            }
        }
        if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
            // End has been reached
            // Do something

            loading = true;
            onLoadMore();
        }
    }

    private void clipToolbarOffset() {
        if (mToolbarOffset > mToolbarHeight) {
            mToolbarOffset = mToolbarHeight;
        }
        else if (mToolbarOffset < 0) {
            mToolbarOffset = 0;
        }
    }

    private void setVisible() {
        if (mToolbarOffset > 0) {
            onShow();
            mToolbarOffset = 0;
        }
        mControlsVisible = true;
    }

    private void setInvisible() {
        if (mToolbarOffset < mToolbarHeight) {
            onHide();
            mToolbarOffset = mToolbarHeight;
        }
        mControlsVisible = false;
    }

    public abstract void onMoved(int distance);

    public abstract void onShow();

    public abstract void onHide();

    public abstract void onLoadMore();
}

And it's implementation is below:

HidingScrollListener scrollListener = new HidingScrollListener(activity, manager) {
         @Override
         public void onMoved(int distance) {

         }

         @Override
         public void onShow() {

         }

         @Override
         public void onHide() {

         }

         @Override
         public void onLoadMore() {
            // you can do your pagination here.
         }
      };
      mRecyclerView.addOnScrollListener(scrollListener);