RecycleView上拉加载更多、实现分批分页功能

RecycleView越来越流行起来,以前的listview终将被ReccylceView所替代,listview实现分批分页加载能添加footView,headView但是RecycleView如何实现呢?

private List rows = new ArrayList<>();
private List rowsList = new ArrayList<>();

 

int lastVisibleItem;
private LinearLayoutManager mLinearLayoutManager;
/**
 * 当前页面的下标
 */
private int pageIndex = 1;

//是否有下一页
private boolean hasNextPage;
@Override
protected void initView(View view) {
    super.initView(view);
    mLinearLayoutManager = new LinearLayoutManager(getContext());
    rvList.setLayoutManager(mLinearLayoutManager);
    adapter = new SystemMessageAdapter();
    rvList.setAdapter(adapter);
  	………………
    lodeData();
    initRegister();
}
private void initRegister() {

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

            lastVisibleItem = mLinearLayoutManager.findLastVisibleItemPosition();
            int itemCount = mLinearLayoutManager.getItemCount();//获取总个数
            if (lastVisibleItem >= itemCount - 2 && dy > 0) {
                if (!hasNextPage) {
                    AppContext.showToast("NO CONTENT");
                } else {

                    //请求网络
                    pageIndex++;
                    lodeData();
                    adapter.notifyDataSetChanged();


                }
            }
        }
    });
}

 

rows.clear();
rows = baseResponse.getRows();

rowsList=rows;

if (hasNextPage) {
    rowsList.addAll(rows);
}

if (rowsList.size() == 0 || rowsList == null) {
    rl_empty.setVisibility(View.VISIBLE);
    swipeLayout.setVisibility(View.GONE);
} else {
    rl_empty.setVisibility(View.GONE);
    swipeLayout.setVisibility(View.VISIBLE);
    adapter.setNewData(rowsList);
}

 

 

你可能感兴趣的:(橘子---圆又圆)