<android.support.v4.widget.SwipeRefreshLayout android:id="@+id/layout_swipe_refresh" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </android.support.v4.widget.SwipeRefreshLayout>
mRefreshLayout.setOnRefreshListener(new OnRefreshListener(){
public void onRefresh() {
//我在List最前面加入一条数据
mData.add(0, "嘿,我是“下拉刷新”生出来的");
//数据重新加载完成后,提示数据发生改变,并且设置现在不在刷新
mAdapter.notifyDataSetChanged();
mRefreshLayout.setRefreshing(false);
}
});
package com.study.wnw.recyclerviewrefresh;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
/** * Created by wnw on 16-5-26. */
public abstract class EndLessOnScrollListener extends RecyclerView.OnScrollListener{
//声明一个LinearLayoutManager
private LinearLayoutManager mLinearLayoutManager;
//当前页,从0开始 private int currentPage = 0;
//已经加载出来的Item的数量
private int totalItemCount;
//主要用来存储上一个totalItemCount
private int previousTotal = 0;
//在屏幕上可见的item数量
private int visibleItemCount;
//在屏幕可见的Item中的第一个
private int firstVisibleItem;
//是否正在上拉数据
private boolean loading = true;
public EndLessOnScrollListener(LinearLayoutManager linearLayoutManager) {
this.mLinearLayoutManager = linearLayoutManager;
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = recyclerView.getChildCount();
totalItemCount = mLinearLayoutManager.getItemCount();
firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();
if(loading){
//Log.d("wnwn","firstVisibleItem: " +firstVisibleItem);
//Log.d("wnwn","totalPageCount:" +totalItemCount);
//Log.d("wnwn", "visibleItemCount:" + visibleItemCount);
if(totalItemCount > previousTotal){
//说明数据已经加载结束
loading = false;
previousTotal = totalItemCount;
}
}
//这里需要好好理解
if (!loading && totalItemCount-visibleItemCount <= firstVisibleItem){
currentPage ++;
onLoadMore(currentPage);
loading = true;
}
}
/**
* 提供一个抽闲方法,在Activity中监听到这个EndLessOnScrollListener
* 并且实现这个方法
* */
public abstract void onLoadMore(int currentPage);}
mRecyclerView.addOnScrollListener(new EndLessOnScrollListener(mLinearLayoutManager) {
@Override
public void onLoadMore(int currentPage) {
loadMoreData();
}
});
//每次上拉加载的时候,给RecyclerView的后面添加了10条数据数据
private void loadMoreData(){
for (int i =0; i < 10; i++){
mData.add("嘿,我是“上拉加载”生出来的"+i);
mAdapter.notifyDataSetChanged();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有