RecyclerView
使用以下代码行,我可以在using中显示来自后端服务器的数据PagedListAdapter
。
movieViewModel = ViewModelProviders.of(this).get(MovieViewModel.class);
MovieAdapter adapter = new MovieAdapter(this);
movieViewModel.moviePagedList.observe(this, adapter::submitList);
recyclerView.setAdapter(adapter);
我创建了这个方法:
private void movieSearch(String searchText) {
globalSearch = searchText;
movieViewModel.replaceSubscription(this);
movieViewModel = ViewModelProviders.of(this).get(MovieViewModel.class);
MovieAdapter adapter = new MovieAdapter(this);
movieViewModel.moviePagedList.observe(this, adapter::submitList);
recyclerView.setAdapter(adapter);
}
从内部调用它onQueryTextChange()
以显示搜索结果,但由于某些原因,我的数据RecyclerView
没有刷新。
这也是我的MovieViewModel
课:
public class MovieViewModel extends ViewModel {
LiveData<PagedList<ApiResponse.Movie>> moviePagedList;
public MovieViewModel() {
MovieDataSourceFactory movieDataSourceFactory = new MovieDataSourceFactory(search);
PagedList.Config config = new PagedList.Config.Builder().setEnablePlaceholders(false).setPageSize(20).build();
moviePagedList = new LivePagedListBuilder<>(movieDataSourceFactory, config).build();
}
void replaceSubscription(LifecycleOwner lifecycleOwner) {
moviePagedList.removeObservers(lifecycleOwner);
}
}
我每次搜索某些东西时,都需要获取新鲜数据。如何解决这个问题?