实际上,我是Paging Library的新手。在这种情况下,我从PagedList观察到ViewModel,即使新闻列表出现在UI上,该返回值始终为零。
viewModel.getNews().observe(this, news -> {
swipeRefreshLayout.setRefreshing(false);
Timber.d("news size is %s",news.size());// **news size is 0**
adapter.submitList(news);
});
我的数据源是
public class NewsDataSource extends ItemKeyedDataSource<String, News> {
private MutableLiveData<Resource.Status> netStatusLive = new MutableLiveData<>();
/*Hide Logic for clean purpose*/
private List<News> localNewsList = new ArrayList<>();
public NewsDataSource( /*Hide Logic for clean purpose*/) {
/*Hide Logic for clean purpose*/
}
@Override
public void loadInitial(@NonNull LoadInitialParams<String> params, @NonNull LoadInitialCallback<News> callback) {
netStatusLive.postValue(Resource.Status.LOADING);
localNewsList.clear();
Disposable disposable = /*Hide Logic for clean purpose*/
compositeDisposable.add(disposable);
}
@Override
public void loadAfter(@NonNull LoadParams<String> params, @NonNull LoadCallback<News> callback) {
if (localNewsList.size() > 19) {
netStatusLive.postValue(Resource.Status.LOADING);
Disposable disposable = /*Hide Logic for clean purpose*/
compositeDisposable.add(disposable);
}
}
private String getLastIdOfNews(List<News> localNewsList) {
if (localNewsList.size() <= 0) {
return "android";
} else {
return localNewsList.get(localNewsList.size() - 1).getId();
}
}
@Override
public void loadBefore(@NonNull LoadParams<String> params, @NonNull LoadCallback<News> callback) {
//do nothing...
}
@NonNull
@Override
public String getKey(@NonNull News item) {
return item.getId();
}
private void onPaginationError(Throwable throwable) {
netStatusLive.postValue(Resource.Status.ERROR);
Timber.e(throwable);
}
public LiveData<Resource.Status> getNetworkState() {
return netStatusLive;
}
}
在这种情况下,要求localNewsList
给出loadAfter
上News的最后一个ID,就像
Observable<List<News>> getNewsList(@Query("skip") int skip,
@Query("limit") int limit,
@Query("lastNewsId") String lastNewsId);
我的DataSourceFactory是
public class NewsDataSourceFactory extends DataSource.Factory<String, Journal> {
private MutableLiveData<JournalDataSource> dataSourceLive = new MutableLiveData<>();
/*Hide Logic for clean purpose*/
public NewsDataSourceFactory( /*Hide Logic for clean purpose*/) {
/*Hide Logic for clean purpose*/
}
@Override
public DataSource<String, Journal> create() {
JournalDataSource dataSource = new JournalDataSource( /*Hide Logic for clean purpose*/);
dataSourceLive.postValue(dataSource);
return dataSource;
}
public void prepareData(String coursId, String studnetId) {
this.coursId = coursId;
this.studnetId = studnetId;
}
public MutableLiveData<JournalDataSource> getDataSource() {
return dataSourceLive;
}
}
ViewModle是
public class NewsViewModel extends ViewModel {
private LiveData<PagedList<News>> newslist = new MutableLiveData<>();
private LiveData<Resource.Status> networkState = new MutableLiveData<>();
private NewsDataSourceFactory factory;
private PagedList.Config config;
private Executor executor;
NewsViewModel(NewsDataSourceFactory factory, PagedList.Config config) {
this.factory = factory;
this.config = config;
executor = Executors.newFixedThreadPool(5);
networkState = Transformations.switchMap(factory.getDataSource(), source -> {
Timber.d("network status get");
return source.getNetworkState();
}
);
}
public void getNews(String courseId, String studentId) {
//Newss = repo.getNewss(auth, courseId, studentId, 0, 20, "android");
factory.prepareData(courseId, studentId);
newslist = new LivePagedListBuilder<>(factory, config).setFetchExecutor(executor).build();
}
public void refresh() {
factory.getDataSource().getValue().invalidate();
}
public LiveData<PagedList<News>> getNews() {
return newslist;
}
public LiveData<Resource.Status> getNetworkState() {
return networkState;
}
我认为,我的问题可能是我的数据源用例和datasource type不匹配。因此PageList无法按预期工作。
答案 0 :(得分:0)
根据Android Paging Library Doc,您应该将Api同步! 如果您使用RxRetrofit,只需删除observeOn()和subscribeOn()即可在分页库使用的当前线程上运行api。 我测试了它,对我来说很好。