当dataSource更改时,如何刷新布局?如果您知道,请告诉我,谢谢。
adapter = new CollectAdapter(context, CollectionFragment.this);
adapter.setHasStableIds(true);
config = new PagedList.Config.Builder()
.setPageSize(8)
.setEnablePlaceholders(false)
.setInitialLoadSizeHint(8)
.build();
liveData = new LivePagedListBuilder(new DataSourceFactory(), config).build();
liveData.observe(this, new Observer<PagedList<Collection>>() {
@Override
public void onChanged(PagedList<Collection> collections) {
adapter.submitList(collections);
}
});
recyvleView = view.findViewById(R.id.rc_collect);
recyvleView.addItemDecoration(new RecycleViewItemDecoration(5, 5));
LinearLayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
recyvleView.setLayoutManager(layoutManager);
recyvleView.setAdapter(adapter);
答案 0 :(得分:0)
您可以通过调用invalidate方法来更改dataSource:
livedata.value.dataSource.invalidate()
答案 1 :(得分:0)
val movieListAdapter : PagingDataAdapter
swiperefresh.setOnRefreshListener {
movieListAdapter.refresh()
swiperefresh.isRefreshing = false
}
答案 2 :(得分:0)
Ahmed Garhy 答案的改进,它限制了垃圾邮件刷卡。这仍然是一个棘手的解决方案,但提供了一些不同的用户体验。
swiperefresh.setOnRefreshListener {
movieListAdapter.refresh()
swiperefresh.isRefreshing = false
}
lifecycleScope.launchWhenCreated {
adapter.addLoadStateListener { loadState ->
//this reveals RV when loading is complete
rv_list.isVisible = loadState.source.refresh is LoadState.NotLoading
///this keeps indicating that data is refreshing now
progress_bar.isVisible = loadState.source.refresh is LoadState.Loading
//this turns off swipeRefresh while updating
swipe_refresh.isEnabled = loadState.source.refresh is LoadState.NotLoading
}
}