【发布时间】:2016-03-07 12:25:12
【问题描述】:
我是回收站视图的新手。我的要求如下:
- 我必须调用一个将提供两个数组的 Web 服务。一个我需要在列表中显示的数据。为此,我使用RecyclerView。另一个数组是状态,我在微调器中显示。此 Web 服务是分页的。我添加了分页,它工作正常。
- 当用户从微调器中选择其他元素时,我必须再次进行 Web 服务调用,并且回收站视图数据应该更改。
目前,在分页的情况下,我正在执行以下操作,一旦我从后续页面获得更多数据:
mAccountListingsAdapter.notifyItemRangeInserted(mAccountListingsAdapter.getItemCount(), mListings.size() - 1);
而且,当我从微调器更改数据时,我正在执行以下操作:
mListings.clear();//Clear the data set
mAccountListingsAdapter.notifyDataSetChanged();//Call notify data set changed on recycler view adapter
getAccountListings();//Fetch new data from the web service and display in recycler view
但是,建议不要直接在回收器视图适配器上调用 notifyDataSetChanged(),而应调用特定的 notifyXXX 方法,以避免性能和动画问题。
所以,我怀疑我是否正确地在onItemSelected() 中通知回收器视图适配器的微调器,或者应该更改它。
附:我尝试关注onItemSelected:
int size = mListings.size();
mListings.clear();
mAccountListingsAdapter.notifyItemRangeRemoved(0, size - 1);
但随后它崩溃了,但有以下异常:
03-02 12:59:41.046: E/AndroidRuntime(4270): java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 4(offset:0).state:5
【问题讨论】:
标签: android android-recyclerview