导入依赖库:
compile 'android.arch.paging:runtime:1.0.1'
使用Paging实现无限滚动,实现清空、更新、初始化功能,并且添加header和footer
public class PagingActivity extends AppCompatActivity {
public class Student{
public int id;
public String name;
}
@BindView(R.id.recyclerViewId)
RecyclerView mRecyclerView;
MyAdpater adpater;
PagedList.Config config;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paging);
ButterKnife.bind(this);
adpater = new MyAdpater();
config = new PagedList.Config.Builder()
.setEnablePlaceholders(false)//配置是否启动PlaceHolders,这里设置是否设置PagedList中的占位符,如果设置为true,我们的数据数量必须固定,由于网络数据数量不固定,所以设置false
.setInitialLoadSizeHint(20)//初始化加载的数量
.setPageSize(10)//配置分页加载的数量
// .setPrefetchDistance(2) //距离底部还有多少条数据时开始预加载
.build();
LiveData> liveData = new LivePagedListBuilder(MyDataSourceFactory,config).build();
liveData.observe(this, new Observer>() {
@Override
public void onChanged(@Nullable PagedList students) {
adpater.submitList(students);
}
});
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(adpater);
}
@OnClick(R.id.clearBtn)
public void onClearClicked(){
adpater.submitList(null);//清空数据
}
@OnClick(R.id.initBtn)
public void onInitClicked(){//初始化数据
LiveData> liveData = new LivePagedListBuilder(MyDataSourceFactory,config).build();
liveData.observe(this, new Observer>() {
@Override
public void onChanged(@Nullable PagedList students) {
adpater.submitList(students);
}
});
}
@OnClick(R.id.updateBtn)
public void onUpdateClicked(){//更新数据
adpater.getCurrentList().get(8).name="修改了姓名";
adpater.notifyItemChanged(8);
}
DataSource.Factory MyDataSourceFactory = new DataSource.Factory() {
@Override
public DataSource create() {
return new MyDataSource();
}
};
class MyDataSource extends PositionalDataSource{
@Override
public void loadInitial(@NonNull LoadInitialParams params, @NonNull LoadInitialCallback callback) {
callback.onResult(loadData(0,20),0,100);//setEnablePlaceholders为false:忽略总计数(非必填),true:总共请求totalCount(必填)
}
@Override
public void loadRange(@NonNull LoadRangeParams params, @NonNull LoadRangeCallback callback) {
// if(params.startPosition<100) {
callback.onResult(loadData(params.startPosition, 10));
// }
}
}
class MyDataSource2 extends PageKeyedDataSource{
@Override
public void loadInitial(@NonNull LoadInitialParams params, @NonNull LoadInitialCallback callback) {
callback.onResult(loadData(0,20),0,100,0,10);
}
@Override
public void loadBefore(@NonNull LoadParams params, @NonNull LoadCallback callback) {
// callback.onResult(loadData(params.key,10),params.key-1);
}
@Override
public void loadAfter(@NonNull final LoadParams params, @NonNull LoadCallback callback) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),params.key/10+"",Toast.LENGTH_SHORT).show();
}
});
callback.onResult(loadData(params.key,10),params.key+10);
}
}
/**
* 假设这里需要做一些后台线程的数据加载任务。
*
* @param startPosition
* @param count
* @return
*/
private List loadData(int startPosition,int count) {
List list = new ArrayList();
for (int i = 0; i < count; i++) {
Student data = new Student();
data.id = startPosition + i;
data.name = "姓名:abc" + data.id;
list.add(data);
}
return list;
}
DiffUtil.ItemCallback diffCallback = new DiffUtil.ItemCallback() {
@Override
public boolean areItemsTheSame(Student oldItem, Student newItem) {
return oldItem.id == newItem.id;
}
@Override
public boolean areContentsTheSame(Student oldItem, Student newItem) {
return oldItem.equals(newItem);
}
};
class MyAdpater extends PagedListAdapter{
public MyAdpater(){
super(diffCallback);
}
@Override
public int getItemViewType(int position) {
int itemCount = getItemCount() - 1;
if(position == 0){
return ITEM_TYPE_HEADER;
}else if(position == itemCount){
return ITEM_TYPE_FOOTER;
}else{
return super.getItemViewType(position);
}
}
@Override
public int getItemCount() {
return super.getItemCount() + 2;
}
@Nullable
@Override
protected Student getItem(int position) {
return super.getItem(position-1);
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
switch(viewType){
case ITEM_TYPE_HEADER:
TextView headerTv = new TextView(getApplicationContext());
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,150);
headerTv.setLayoutParams(params);
headerTv.setTextSize(28);
headerTv.setText("I am header");
headerTv.setTextColor(Color.WHITE);
headerTv.setBackgroundColor(Color.BLUE);
headerTv.setGravity(Gravity.CENTER);
return new HeaderViewHolder(headerTv);
case ITEM_TYPE_FOOTER:
TextView footTv = new TextView(getApplicationContext());
footTv.setText("I am footer");
return new FooterViewHolder(footTv);
default:
View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.list_item_layout,null);
return new MyViewHolder(view);
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if(holder instanceof MyViewHolder) {
((MyViewHolder)holder).nameTv.setText(getItem(position).name);
((MyViewHolder)holder).idTv.setText(getItem(position).id + "");
}
}
//添加header和footer,重写AdapterDataObserver
@Override
public void registerAdapterDataObserver(@NonNull RecyclerView.AdapterDataObserver observer) {
super.registerAdapterDataObserver(new AdapterDataObserverProxy(observer,1));
}
private static final int ITEM_TYPE_HEADER = -99;
private static final int ITEM_TYPE_FOOTER = -100;
}
class MyViewHolder extends RecyclerView.ViewHolder{
public TextView nameTv,idTv;
public MyViewHolder(View itemView) {
super(itemView);
nameTv = itemView.findViewById(R.id.nameTv);
idTv = itemView.findViewById(R.id.idTv);
}
}
class HeaderViewHolder extends RecyclerView.ViewHolder{
public HeaderViewHolder(View itemView) {
super(itemView);
}
}
class FooterViewHolder extends RecyclerView.ViewHolder{
public FooterViewHolder(View itemView) {
super(itemView);
}
}
}
重写AdapterDataObserver
// 1.新建一个 AdapterDataObserverProxy 类继承 RecyclerView.AdapterDataObserver
class AdapterDataObserverProxy extends RecyclerView.AdapterDataObserver {
RecyclerView.AdapterDataObserver adapterDataObserver;
int headerCount;
public AdapterDataObserverProxy(RecyclerView.AdapterDataObserver adapterDataObserver, int headerCount) {
this.adapterDataObserver = adapterDataObserver;
this.headerCount = headerCount;
}
@Override
public void onChanged() {
adapterDataObserver.onChanged();
}
@Override
public void onItemRangeChanged(int positionStart, int itemCount) {
adapterDataObserver.onItemRangeChanged(positionStart + headerCount, itemCount);
}
@Override
public void onItemRangeChanged(int positionStart, int itemCount, @Nullable Object payload) {
adapterDataObserver.onItemRangeChanged(positionStart + headerCount, itemCount, payload);
}
// 当第n个数据被获取,更新第n+1个position
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
adapterDataObserver.onItemRangeInserted(positionStart + headerCount, itemCount);
}
@Override
public void onItemRangeRemoved(int positionStart, int itemCount) {
adapterDataObserver.onItemRangeRemoved(positionStart + headerCount, itemCount);
}
@Override
public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
super.onItemRangeMoved(fromPosition + headerCount, toPosition + headerCount, itemCount);
}
}
参考:https://blog.csdn.net/mq2553299/article/details/89075709