compile 'com.androidkun:pulltorefreshrecyclerview:1.1.0'
<com.androidkun.PullToRefreshRecyclerView
android:id="@+id/pullToRefreshRV"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
pullToRefreshRV = (PullToRefreshRecyclerView) findViewById(R.id.pullToRefreshRV);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
pullToRefreshRV.setLayoutManager(layoutManager);
adapter = new ModeAdapter(this, R.layout.item_mode,data);
pullToRefreshRV.setAdapter(adapter);
//是否开启下拉刷新功能
pullToRefreshRV.setPullRefreshEnabled(true);
//是否开启上拉加载功能
pullToRefreshRV.setLoadingMoreEnabled(true);
//设置是否显示上次刷新的时间
pullToRefreshRV.displayLastRefreshTime(true);
//设置刷新回调
pullToRefreshRV.setPullToRefreshListener(this);
//主动触发下拉刷新操作
//pullToRefreshRV.onRefresh();
GridLayoutManager gridLayoutManager = new GridLayoutManager(this,2);
recyclerView.setLayoutManager(gridLayoutManager);
@Override
public void onRefresh() {
pullToRefreshRV.postDelayed(new Runnable() {
@Override
public void run() {
pullToRefreshRV.setRefreshComplete();
//模拟没有数据的情况
data.clear();
adapter.notifyDataSetChanged();
}
}, 3000);
}
@Override
public void onLoadMore() {
pullToRefreshRV.postDelayed(new Runnable() {
@Override
public void run() {
pullToRefreshRV.setLoadMoreComplete();
//模拟加载数据的情况
int size = data.size();
for (int i = size; i < size + 4; i++) {
data.add("" + i + i + i + i);
}
adapter.notifyDataSetChanged();
}
}, 3000);
}
public class ModeAdapter extends BaseAdapter {
public ModeAdapter(Context context, int layoutId, List datas) {
super(context, layoutId, datas);
}
@Override
public void convert(ViewHolder holder, Object o) {
holder.setText(R.id.textView, (String) o);
}
}