I. Overall thinking:
Create new data sources to load old data and add/delete data. Add and delete the List collection, return the latest List data to PagedList through loadInitial, and finally call submitList to update the data.
PageKeyedDataSource as data source, for example, first create a new DataSource class PageKeyedDataSource PageKeyedDataSourceExtension inheritance,
Create a List in the new DataSource to load the new data, which is also a collection for adding and deleting data later.
public List<ExBean> mDataList = new ArrayList<>();
Copy the code
In the implemented loadInitial() method, the latest mDataList data is returned to the PagedList via LoadInitialCallback, loadBefore and loadAfter simply pass in an empty List.
@Override
public void loadInitial(@NonNull LoadInitialParams<Integer> params, @NonNull LoadInitialCallback<Integer, ExBean> callback) {
callback.onResult(mDataList, null.null);
}
@Override
public void loadBefore(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, ExBean> callback) {
callback.onResult(Collections.<ExBean>emptyList(), null);
}
@Override
public void loadAfter(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, ExBean> callback) {
callback.onResult(Collections.<ExBean>emptyList(), null);
}
Copy the code
In PageKeyedDataSourceExtension to create a new PagedList to rebind Adapter,
public PagedList<ExBean> createNewPagedList(PagedList.Config config) {
@SuppressLint("RestrictedApi") PagedList<ExBean> pagedList
= new PagedList.Builder<>(this. config) .setFetchExecutor(ArchTaskExecutor.getIOThreadExecutor()) .setNotifyExecutor(ArchTaskExecutor.getMainThreadExecutor()) .build();return pagedList;
}
Copy the code
With the new data source ready, you can start adding and deleting.
Second, the new
/ / add
public void add(ExBean exBean) {
// Get the current PagedList
PagedList<ExBean> curList = getCurrentList();
PageKeyedDataSourceExtension source = new PageKeyedDataSourceExtension();
// Add the current data and the data to be added to the new dataset
source.mDataList.addAll(curList);
source.mDataList.add(exBean);
// Create a new data PagedList
PagedList<ExBean> pagedList = source.createNewPagedList(curList.getConfig());
submitList(pagedList);
}
Copy the code
Gets the current PagedList and add it to the List of PageKeyedDataSourceExtension collection, also add a new data exBean, after get the new data, CreateNewPagedList (new PagedList.Builder<>) triggers loadInitial, returns a new PagedList, and finally binds new data with submitList.
Three, delete,
Delete and add is the same reason, using the PagedListAdapter getItem(int positon) to obtain the data to be deleted, through traversing to eliminate the data to be deleted, create a new data source and bind to the Adapter again.
/ / delete
public void delete(int position) {
// Get the current PagedList
PagedList<ExBean> curList = getCurrentList();
PageKeyedDataSourceExtension source = new PageKeyedDataSourceExtension();
ExBean b = getItem(position);
// Iterate through the current PagedList to eliminate items that need to be deleted
for(ExBean bean : curList){
if(bean != b){
source.mDataList.add(bean);
}
}
// Create a new data PagedList
PagedList<ExBean> pagedList = source.createNewPagedList(curList.getConfig());
submitList(pagedList);
}
Copy the code
Take a look at the effect:
Other issues
In addition and deletion, the sliding of Paging is taken over by the new data source as a new data source is created and bound to the Adapter.
Can know from code PageKeyedDataSourceExtension class loadAfter, introduced into an empty List collection,
. @Override publicvoid loadAfter(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, ExBean> callback) {
callback.onResult(Collections.<ExBean>emptyList(), null); }...Copy the code
As a result, after adding or deleting data, if the user continues to swipe down to request data, no data will be returned.
If there is a requirement to continue sliding requests after additions or deletions, this requires special handling. LoadAfter (DataSource) loadAfter (DataSource) loadAfter (DataSource) loadAfter (DataSource)
@Override
public void loadAfter(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, ExBean> callback) {
//callback.onResult(Collections.<ExBean>emptyList(), null);
mDataSource.loadAfter(params,callback)//mDataSource is the old data source
}
Copy the code