简体   繁体   English

Android:从微调框选择的项目上的“更新回收站”视图所需的建议

[英]Android: Suggestion required for Update recycler view on item selected from spinner

I am new to recycler view. 我对回收者视图不熟悉。 My requirement is as follows: - I have to call a web service that will give two arrays. 我的要求如下:-我必须调用一个将提供两个数组的Web服务。 One with data I need to show in the list. 我需要在列表中显示的数据之一。 For this purpose, I am using RecyclerView . 为此,我正在使用RecyclerView The other array is of statuses, which I am showing in spinner. 另一个数组是状态,我将在微调器中显示这些状态。 This web service is paginated. 该Web服务是分页的。 I have added pagination and it is working fine. 我添加了分页功能,并且工作正常。 - When user selects some other element from the spinner, again I have to make a web service call and recycler view data should change. -当用户从微调器中选择其他元素时,我又必须进行Web服务调用,并且回收者视图数据应该更改。 Currently, in case of pagination I am doing following, once I get more data from the successive pages: 当前,在分页的情况下,一旦从后续页面中获取了更多数据,我就会执行以下操作:

mAccountListingsAdapter.notifyItemRangeInserted(mAccountListingsAdapter.getItemCount(), mListings.size() - 1);

And, when I change data from spinner, I am doing following: 而且,当我从微调器更改数据时,我正在执行以下操作:

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

But, it is suggested that, instead of calling notifyDataSetChanged() directly on recycler view adapter, one should call specific notifyXXX method, to avoid performance and animations issues. 但是,建议不要使用直接在回收器视图适配器上调用notifyDataSetChanged()的方法,而应调用特定的notifyXXX方法,以避免性能和动画问题。

So, I am under doubt, if I am doing right to notify recycler view adapter in onItemSelected() of spinner, or it should be changed. 因此,我是否在正确地通知微调器的onItemSelected()的回收者视图适配器是否正确,还是应该更改它。

PS I tried following in the onItemSelected : PS我尝试以下onItemSelected

int size = mListings.size();
mListings.clear();
mAccountListingsAdapter.notifyItemRangeRemoved(0, size - 1);

But then it crashed, with following exception: 但是随后它崩溃了,但以下情况除外:

03-02 12:59:41.046: E/AndroidRuntime(4270): java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 4(offset:0).state:5

I think notifyItemRangeRemoved is the correct method to use here, but the value you are passing for second parameter is wrong. 我认为notifyItemRangeRemoved是此处使用的正确方法,但是您传递给第二个参数的值是错误的。 As per the doc, the second parameter is number of items removed from data set, what you are passing is the position of last item removed. 根据文档,第二个参数是从数据集中删除的项目数,您传递的是最后删除的项目的位置。

在此处输入图片说明

So the below code should work fine 所以下面的代码应该可以正常工作

int size = mListings.size();
mListings.clear();
mAccountListingsAdapter.notifyItemRangeRemoved(0, size);

For more info refer : http://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#notifyItemRangeRemoved(int,%20int) 有关更多信息,请参见: http : //developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#notifyItemRangeRemoved(int,%20int)

First of all, the method definition for notifyItemRangeRemoved (int, int) is: 首先, notifyItemRangeRemoved (int, int)的方法定义为:

public final void notifyItemRangeRemoved (int positionStart, int itemCount)

The second argument is the count , not positionEnd . 第二个参数是count ,而不是positionEnd In your case you are passing size - 1 as the second argument, when it should be size itself. 在您的情况下,您将传递size - 1作为第二个参数,此时它应该是size本身。

int size = mListings.size();
mListings.clear();
// should be notifyItemRangeRemoved(0, size)
mAccountListingsAdapter.notifyItemRangeRemoved(0, size - 1);

Second, notifyDataSetChanged() is frowned upon because it triggers a rebind and relayout of all visible views. 其次, notifyDataSetChanged()不受欢迎,因为它触发了所有可见视图的重新绑定和重新布局。 In your case, where the number of visible items is zero, I don't see why notifyDataSetChanged() would degrade performance. 在您的情况下,可见项的数量为零,我看不到为什么notifyDataSetChanged()会降低性能。 If you are animating the removal of items, go with notifyItemRangeRemoved(0, size) . 如果要对删除项设置动画效果,请使用notifyItemRangeRemoved(0, size) Else, its quite alright to use notifyDataSetChanged() over here. 否则,完全可以在此处使用notifyDataSetChanged()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM