Android 如何从PagedListAdapter刷新RecyclerView中的数据?
使用以下代码行,我可以使用Android 如何从PagedListAdapter刷新RecyclerView中的数据?,android,android-recyclerview,android-pageradapter,android-paging,android-paging-library,Android,Android Recyclerview,Android Pageradapter,Android Paging,Android Paging Library,使用以下代码行,我可以使用PagedListAdapter在RecyclerView中显示来自后端服务器的数据 movieViewModel = ViewModelProviders.of(this).get(MovieViewModel.class); MovieAdapter adapter = new MovieAdapter(this); movieViewModel.moviePagedList.observe(this, adapter::submitList); recyclerV
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()
以显示搜索结果,但由于某些原因,myRecyclerView
中的数据未刷新
这也是我的moviewModel
课程:
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);
}
}
公共类MovieViewModel扩展了ViewModel{
LiveData电影页面列表;
公共电影视图模型(){
MovieDataSourceFactory MovieDataSourceFactory=新的MovieDataSourceFactory(搜索);
PagedList.Config Config=new PagedList.Config.Builder().setEnablePlaceholders(false).setPageSize(20.build();
moviePagedList=newlivepagedlistbuilder(movieDataSourceFactory,config).build();
}
作废替换订阅(LifecycleOwner LifecycleOwner){
moviePagedList.removeObservers(生命周期所有者);
}
}
我每次搜索某个东西时,都需要获取freash数据。如何解决这个问题?
PagedListAdapter
是android.support.v7.widget.RecyclerView.Adapter的一个子类,所以调用notifyDataSetChanged()
就可以了。有了这个答案,我将向您展示我是如何过滤适配器中的项的。不是直接回答你的问题,而是给你另一个解决方案
首先,我在我的工具栏中有一个名为m\u app\u bar\u title\u txt的EditText
,在我的活动中,我调用以下内容:
//I'm using TextWatcher to see when text changes
m_app_bar_title_txt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
//If the text changes I call the following method
//Passing the text to the metod
filter(s.toString());
}
});
private void filter(String s) {
ArrayList<ScoresData> aData = new ArrayList<>();
for (ScoresData mData : data){
if (mData.textPlayerName.toLowerCase().contains(s.toLowerCase())){
aData.add(mData);
}
}
//this method is in my RecyclerView.Adapter class
//I will provide this below
mAdapter.filterList(aData);
}
如果您想知道ScoresData
是什么样子的
public class ScoresData {
public String mImage;
public String textPlayerName;
public String textPos;
public String textTotalScore;
public String textPlayed;
public String textRounds;
}
我希望这有帮助。这个问题可以通过打电话来解决
movieViewModel.moviePagedList.dataSource.invalidate()
在您的活动/片段中。这写在
页面列表/数据源对是数据集的快照如果发生更新,例如重新排序、插入、删除或内容更新,则必须创建一对新的页面列表/数据源。数据源必须检测到它无法继续加载其快照(例如,当数据库查询注意到一个表正在失效时),并调用invalidate()。然后将创建一个新的PagedList/DataSource对,以从数据库查询的新状态加载数据
我想这就是答案。调用适配器。刷新()
注意movieListAdapter应该扩展PagingDataAdapter适配器。notifyDataSetChanged()代码>@HB。谢谢,但它不适用于PagedListAdapter
:(.我使用2个ArrayList,一个用于过滤,另一个用于显示所有项目。@HB.让我尝试使用2个ArrayList。我知道这一点,但它不起作用。你有其他想法吗?正如你所说,它没有回答我的问题,但它帮助我解决了问题。我最终使用了你的想法和来自此的答案。投票通过!非常感谢!
movieViewModel.moviePagedList.dataSource.invalidate()
swiperefresh.setOnRefreshListener {
movieListAdapter.refresh()
swiperefresh.isRefreshing = false
}