【发布时间】:2018-11-21 13:20:12
【问题描述】:
使用以下代码行,我可以使用PagedListAdapter 在RecyclerView 中显示来自后端服务器的数据。
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);
}
}
我每次搜索某些内容时,都需要获取最新数据。如何解决?
【问题讨论】:
-
adapter.notifyDataSetChanged(); -
@HB。谢谢,但它不适用于
PagedListAdapter:(. -
我用了 2 个 ArrayList,一个用于过滤,一个用于显示所有项目。
-
@HB。让我试试 2 ArrayList。
标签: android android-recyclerview android-pageradapter android-paging android-paging-library