Talking about Android adding monitoring and data confusion to recyclerview

Add listening for recyclerview

1. Write the internal interface in the implemented myadapter:

2. Continue to add the following code in myadapter:

The above code is the general writing method of the interface, which is not difficult. What needs to be noted is several local refresh methods.

3. In order to maximize performance, when the data of an item in the recyclerview view changes, the recyclerview.adapter provides several methods to locally refresh the data view:

In consideration of efficiency, officials suggest replacing this method with a more accurate method (such as the above three methods):

① There are two situations when position (subscript) is mentioned in recyclerview.viewholder:

i. Returned by the getlayoutposition() method of viewholder is the subscript of ItemView in the view

i. The index returned by getadapterposition () method of viewholder is the data element of adapter dataset (such as ArrayList)

Under normal conditions, the contents of data are displayed on the first ItemView, and they correspond to each other one by one.

② However, when using several methods of local refresh, we should note that when a data set, such as ArrayList, removes an element, the elements behind it will be filled forward, so the subscript of each element behind it will be reduced by one;

③ However, for getlayoutposition (), when we remove an ItemView with the notifyiteminserted method, the itemviews behind the view will squeeze up, but their subscripts remain unchanged. For example, if I remove the third of ten itemviews (subscripts 0 to 9) with the notifyiteminserted method, then the subscripts of the remaining itemviews in the view are (0,1 and 3 to 9). At this time, calling remove (9) on the dataset is bound to throw an array out of bounds exception;

④ . therefore, after each refresh, use notifyitemrangechanged (int positionstart, int itemcount) to adjust the subscript of ItemView so that it always corresponds to the subscript of the dataset. Where positionstart is the first changed ItemView subscript, and itemcount refers to the number of itemviews whose subscripts also need to be changed

The above is the primary specific usage of recyclerview.

This talk about Android's increasing monitoring and data confusion for recyclerview is all that Xiaobian has shared with you. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>