【问题标题】:How to refresh data in a RecyclerView from a PagedListAdapter?如何从 PagedListAdapter 刷新 RecyclerView 中的数据?
【发布时间】:2018-11-21 13:20:12
【问题描述】:

使用以下代码行,我可以使用PagedListAdapterRecyclerView 中显示来自后端服务器的数据。

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


【解决方案1】:

这个问题可以通过调用来解决

movieViewModel.moviePagedList.dataSource.invalidate()

在您的活动/片段中。

【讨论】:

    【解决方案2】:

    有了这个答案,我将向您展示我是如何过滤 Adapter 中的项目的。不直接回答你的问题,而是给你另一种解决方案。

    首先,我的ToolBar 中有一个EditText,名为m_app_bar_title_txt,在我的Activity 中,我调用以下代码:

    //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);
    
    }
    

    mAdapter.filterList(aData); 将过滤后的ArrayList 传递给我的RecyclerView.Adapter 中的以下方法:

    public void filterList(ArrayList<ScoresData> filteredList){
        //Changing the original ArrayList -> data to filteredList
        //Then notifying that the list has changed
        data = filteredList;
        notifyDataSetChanged();
    }
    

    如果你想知道ScoresData 长什么样......

    public class ScoresData {
        public String mImage;
        public String textPlayerName;
        public String textPos;
        public String textTotalScore;
        public String textPlayed;
        public String textRounds;
    }
    

    我希望这会有所帮助..

    【讨论】:

    • 正如你所说,没有回答我的问题,但它帮助我解决了它。我终于用你的想法和这个post的答案做到了。投票赞成!非常感谢!
    【解决方案3】:

    PagedListAdapterandroid.support.v7.widget.RecyclerView.Adapter 的子类,因此调用notifyDataSetChanged() 即可。

    【讨论】:

    • 我知道,但它不起作用。你还有什么想法吗?
    【解决方案4】:

    这是写在DataSource的文档中

    PagedList / DataSource 对是数据集的快照如果发生更新,例如重新排序、插入、删除或内容更新,则必须创建一对新的 PagedList / DataSource。 DataSource 必须检测到它不能继续加载它的快照(例如,当数据库查询注意到一个表被无效时),并调用 invalidate()。然后将创建一个新的 PagedList / DataSource 对以从数据库查询的新状态加载数据。

    我想这就是答案。

    【讨论】:

      【解决方案5】:

      调用adapter.refresh()

      swiperefresh.setOnRefreshListener {
        movieListAdapter.refresh()
        swiperefresh.isRefreshing = false
      }
      

      注意 movieListAdapter 应该扩展 PagingDataAdapter

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多