如何使用新的分页库实现 SwipeRefreshLayout
How to implement SwipeRefreshLayout with the new Paging Library
我得到一个 activity 向用户显示项目列表并且它使用分页库。我的问题是当用户向下滑动屏幕时我无法重新加载列表,以便它再次从服务器获取数据。
这是我的数据源工厂:
public class CouponListDataSourceFactory extends DataSource.Factory {
private CouponListDataSource dataSource;
public CouponListDataSourceFactory(CouponRepository repository, String token, String vendorId) {
dataSource = new CouponListDataSource(repository, token, vendorId);
}
@Override
public DataSource create() {
return dataSource;
}
}
下面是我创建 PagedList 的方法
PagedList.Config config = new PagedList.Config.Builder()
.setInitialLoadSizeHint(15)
.setPageSize(10)
.build();
LiveData<PagedList<Coupon>> couponsLiveData = new LivePagedListBuilder<>(dataSourceFactory, config).build();
调用mDataSource.invalidate()方法后,mDataSource会失效,通过DataSource.Factory.create()方法创建新的DataSource实例,所以每次在 DataSource.Factory.create() 方法中提供新的 DataSource() 实例很重要,不要每次都提供相同的 DataSource 实例 .
mDataSource.invalidate() 不起作用, 因为在失效之后,CouponListDataSourceFactory 提供相同的、已经失效的 DataSource 实例。
修改后的CouponListDataSourceFactory会变成下面的样子,调用mCouponListDataSourceFactory.dataSource.invalidate()方法会刷新,或者 我们可以在 LiveData< PagedList < CouponModel > >.getValue().getDataSource().invalidate()[=11 上调用 invalidate 方法,而不是将 dataSource 实例保留在工厂内=]
public class CouponListDataSourceFactory extends DataSource.Factory {
private CouponListDataSource dataSource;
private CouponRepository repository;
private String token;
private String vendorId;
public CouponListDataSourceFactory(CouponRepository repository, String token, String vendorId) {
this.repository = repository;
this.token = token;
this.vendorId = vendorId;
}
@Override
public DataSource create() {
dataSource = new CouponListDataSource(repository, token, vendorId);
return dataSource;
}
}
在 ViewModel 中添加方法 class
public void refresh() {
itemDataSourceFactory.getItemLiveDataSource().getValue().invalidate();
}
从 Activity/Fragment 你可以使用
swipeRefreshLayout.setOnRefreshListener(() -> yourviewModel.refresh());
加载 reyclerView 时隐藏刷新布局
yourViewModel.itemPagedList.observe(this, allProposalModel -> {
mAdapter.submitList(model);
swipeRefreshLayout.setRefreshing(false); //here..
});
我得到一个 activity 向用户显示项目列表并且它使用分页库。我的问题是当用户向下滑动屏幕时我无法重新加载列表,以便它再次从服务器获取数据。
这是我的数据源工厂:
public class CouponListDataSourceFactory extends DataSource.Factory {
private CouponListDataSource dataSource;
public CouponListDataSourceFactory(CouponRepository repository, String token, String vendorId) {
dataSource = new CouponListDataSource(repository, token, vendorId);
}
@Override
public DataSource create() {
return dataSource;
}
}
下面是我创建 PagedList 的方法
PagedList.Config config = new PagedList.Config.Builder()
.setInitialLoadSizeHint(15)
.setPageSize(10)
.build();
LiveData<PagedList<Coupon>> couponsLiveData = new LivePagedListBuilder<>(dataSourceFactory, config).build();
调用mDataSource.invalidate()方法后,mDataSource会失效,通过DataSource.Factory.create()方法创建新的DataSource实例,所以每次在 DataSource.Factory.create() 方法中提供新的 DataSource() 实例很重要,不要每次都提供相同的 DataSource 实例 .
mDataSource.invalidate() 不起作用, 因为在失效之后,CouponListDataSourceFactory 提供相同的、已经失效的 DataSource 实例。
修改后的CouponListDataSourceFactory会变成下面的样子,调用mCouponListDataSourceFactory.dataSource.invalidate()方法会刷新,或者 我们可以在 LiveData< PagedList < CouponModel > >.getValue().getDataSource().invalidate()[=11 上调用 invalidate 方法,而不是将 dataSource 实例保留在工厂内=]
public class CouponListDataSourceFactory extends DataSource.Factory {
private CouponListDataSource dataSource;
private CouponRepository repository;
private String token;
private String vendorId;
public CouponListDataSourceFactory(CouponRepository repository, String token, String vendorId) {
this.repository = repository;
this.token = token;
this.vendorId = vendorId;
}
@Override
public DataSource create() {
dataSource = new CouponListDataSource(repository, token, vendorId);
return dataSource;
}
}
在 ViewModel 中添加方法 class
public void refresh() {
itemDataSourceFactory.getItemLiveDataSource().getValue().invalidate();
}
从 Activity/Fragment 你可以使用
swipeRefreshLayout.setOnRefreshListener(() -> yourviewModel.refresh());
加载 reyclerView 时隐藏刷新布局
yourViewModel.itemPagedList.observe(this, allProposalModel -> {
mAdapter.submitList(model);
swipeRefreshLayout.setRefreshing(false); //here..
});