实际上,我对分页库还很新。这是情况,我正在观察来自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
时提供新闻的最后一个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;
}
我认为,我的问题可能是我的数据源用例与数据源类型不匹配。因此,Pagelist不能像我们预期的那样正常工作。
ViewModel
吗? - Zwal Pyae Kyaw