学习目标:
JetPack之Paging使用
学习内容:
- 两个字 : 分页加载
Paging 三大核心
-
DataSource
数据源。数据存放地。 -
PageList
存放每一页的数据的集合。每请求一页都会加载到这个集合里面。 -
PageListAdapter
与RecyclerView的Adapter差不多。不过我们在使用分页的功能时要继承(extends)PagedListAdapter.
引入Paging
//paging 核心库
implementation 'androidx.paging:paging-runtime:2.1.2'
//
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
需要一个实体类 Concert
class Concert {
String content;
String title;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
其次我们需要一个DataSource(数据存放地)
/**
* 用来创建DataSource对象
*/
public class ConcertFactory extends DataSource.Factory<Integer, Concert> {
private MutableLiveData<ConcertDataSource> mSourceLiveData =
new MutableLiveData<>();
@Override
public DataSource<Integer, Concert> create() {
ConcertDataSource concertDataSource = new ConcertDataSource();
mSourceLiveData.postValue(concertDataSource);
return concertDataSource;
}
}
/**
* 数据源
*/
public class ConcertDataSource extends PositionalDataSource<Concert> {
/**
* 初始化第一页的数据会调用的方法
* @param params
* @param callback
*/
@Override
public void loadInitial(@NonNull LoadInitialParams params, @NonNull
final LoadInitialCallback<Concert> callback) {
callback.onResult(generateItems(0, 20), 0, 1000);
}
/**
* 加载更多也调用的方法
* @param params
* @param callback
*/
@Override
public void loadRange(@NonNull LoadRangeParams params, @NonNull
final LoadRangeCallback<Concert> callback) {
callback.onResult(generateItems(params.startPosition, params.loadSize));
}
private List<Concert> generateItems(int startPosition, int pageSize) {
List<Concert> list = new ArrayList<>();
for (int i = startPosition; i < startPosition + pageSize; i++) {
Concert concert = new Concert();
concert.setTitle("title = " + i);
concert.setContent("content = " + i);
list.add(concert);
}
return list;
}
}
public class ConcertViewModel extends ViewModel {
//一页数据源
private final LiveData<PagedList<Concert>> convertList;
//所有数据源
private DataSource<Integer,Concert> concertDataSource;
public ConcertViewModel(){
ConcertFactory concertFactory = new ConcertFactory();
concertDataSource = concertFactory.create();
//20:每页的数据个数
convertList = new LivePagedListBuilder<>(concertFactory, 20).build();
}
public void invalidateDataSource() {
concertDataSource.invalidate();
}
public LiveData<PagedList<Concert>> getConvertList() {
return convertList;
}
}
适配器
//一定要继承PagedListAdapter
public class MyRecyclerAdapter extends PagedListAdapter<Concert, MyRecyclerAdapter.RecyclerViewHolder> {
//这个跟之前的recycler的adapter不一样。
protected MyRecyclerAdapter() {
super(DIFF_CALLBACK);
}
@NonNull
@Override
public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recyclerview, parent, false);
return new RecyclerViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
Concert concert = getItem(position);
if (concert != null) {
holder.mTitleTextView.setText(concert.getTitle());
holder.mAuthorTextView.setText(concert.getAuthor());
holder.mContentTextView.setText(concert.getContent());
}
}
public static class RecyclerViewHolder extends RecyclerView.ViewHolder {
TextView mTitleTextView;
TextView mAuthorTextView;
TextView mContentTextView;
public RecyclerViewHolder(View itemView) {
super(itemView);
mTitleTextView = itemView.findViewById(R.id.title);
mAuthorTextView = itemView.findViewById(R.id.author);
mContentTextView = itemView.findViewById(R.id.content);
}
}
private static DiffUtil.ItemCallback<Concert> DIFF_CALLBACK =
new DiffUtil.ItemCallback<Concert>() {
//调用以检查两个对象是否表示同一项。
@Override
public boolean areItemsTheSame(Concert oldConcert, Concert newConcert) {
return oldConcert.getTitle().equals(newConcert.getTitle());
}
//调用此选项以检查两个项目是否具有相同的数据。
@SuppressLint("DiffUtilEquals")
@Override
public boolean areContentsTheSame(Concert oldConcert,
Concert newConcert) {
return oldConcert.equals(newConcert);
}
};
}
MainActivity中
final MyRecyclerAdapter adapter = new MyRecyclerAdapter();
ConcertViewModel viewModel =
ViewModelProviders.of(this).get(ConcertViewModel.class);
viewModel.getConvertList().observe(this, new Observer<PagedList<Concert>>() {
@Override
public void onChanged(PagedList<Concert> concerts) {
adapter.submitList(concerts);
}
});
recyclerview.setAdapter(adapter);
recyclerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
到这儿使用就结束了,更加详细的使用请参考官方文档,毕竟paging用来做分页比较方便。