Detailed explanation of listview reuse mechanism of Android learning notes

PS: I haven't blogged for almost three months... I was busy a while ago... I can finally find time to study other things

1. Reuse mechanism of listview

Listview is a control we often use. Although we can use it all, we don't necessarily know the reuse mechanism of listview. Although recycleview is provided after Android version 5.0 to replace listview and GridView, providing a plug-in experience, that is, the so-called modularization. This article mainly discusses the reuse mechanism of listview, so recycleview is proposed. Yesterday, I read a blog about the in-depth analysis of the listview principle of Guo Lin's great God, so I studied for some time and said my understanding.

i. Fundamentals of recyclebin

First, we need to talk about the basic principle of recyclebin. This class is also the key class to realize reuse. Then we need to clarify the concept of activeview. Activityview is actually a view visible on the UI screen (onscreen view) and a view that interacts with users. These views will be directly stored in the mmactivityview array through recyclebin for direct reuse. When we slide the listview, some views will be slid to off screen views, Then these views become scrapviews, that is, abandoned views, which can no longer interact with users, so there is no need to draw these useless views when the UI view changes. It will be stored in the mscrapview array by recyclebin, but it has not been destroyed. The purpose is for secondary reuse, that is, indirect reuse. When a new view needs to be displayed, first judge whether it exists in the mcactivityview. If it exists, we can directly take out reuse from the mcactivityview array, that is, direct reuse. Otherwise, judge from the mscrapview array. If it exists, reuse the current view twice. If it does not exist, we need to expand view.

This is an overall flow chart, and the reuse mechanism is like this. Let's first understand what the listview does when it is loaded for the first time. First, we will execute the onlayout method..

Here we can see that the onlayout method will call the layoutchildren() method, that is, the process of laying out items. The layoutchildren() method will not paste. The amount of code is too long. We just need to know that this is a way to lay out the child views in the listview. When we load the listview for the first time, The array in recyclebin does not have any data, so the first loading requires an inflate view, that is, to create a new view. And the data is loaded from top to bottom when it is loaded for the first time, so the fillfromtop () method will be executed in layoutchildren (). Fillfromtop() will execute the filledown() method.

There is a key method here, that is, the makeandaddview () method, which is the core part of listview displaying items, and this part involves the reuse of listview

Here we can see that if the data source does not change, we will judge whether there are views that can be reused directly from the mmactivityview array. Many readers may not understand the process of direct reuse. For example, we can display 10 pieces of data on a listview page, so we can slide an item distance at this time, In other words, remove the item with position = 0 from the screen and move the item with position = 10 into the screen. Can the item with position = 1 be directly obtained from the mmactivityview array? This is OK. When we load the item data for the first time, we have added the items with position = 0 ~ 9 to the m activityview array. Then, when we load the item data for the second time, because the item with position = 1 is still an activityview, we can get it directly from the array and then rearrange it. This means the direct reuse of items.

If we can't get the view corresponding to position in the mmactivityview array, try to get it from the msrapview abandoned view array. Take the example just now. When the item with position = 0 is removed from the screen, we will first detach the view from the view, empty the children, and then add the abandoned view to the msrapview array, When an item with position = 10 is loaded, the mmactivityview array certainly does not exist and cannot be obtained. Similarly, there is no abandoned view corresponding to position = 10 in the mscrapview. In other words, the mscrapview array only has the data of mscrapview [0], and there must be no data of mscrapview [10], so we think so, You must get new data from the getview method in the adapter. In fact, this is not the case. Although there is no corresponding abandoned view in the mscrapview, the last cached view will be returned and passed to the convertview. That is, return the view corresponding to mscrapview [0]. This is the overall process.

Here we can see that the listview will always only inflate one page of items in the getview method, that is, the number of times the new view will only execute one page of items. Subsequent items are completed through direct reuse and indirect reuse.

Note a situation: for example, if the item with position = 0 is still a one page item, but the item with position = 0 does not completely slide out of the UI, and the item with position = 10 does not fully enter the UI, the item with position = 0 will not be detached, nor will it be added to the abandoned view array. At this time, the mscrapview is empty and has no data, Then the item with position = 10 cannot reuse the view directly from the mcactivityview because it is loaded for the first time. The mmactivityview [10] does not exist, and the mscrapview is empty, so the item with position = 10 can only regenerate the view, that is, inflate from the getview method. The obtainview method is not specifically posted here. You can go in and have a look. Obtainview is actually used to judge whether a view can be obtained from an abandoned view. If it is obtained, execute:

If you can get it here, getview will pass the scrapview. Otherwise:

If it cannot be obtained, null will be passed, and the methods in the adapter defined by us will be executed.

As for sliding up, other methods will be implemented, that is, spreading listview from bottom to top, which will also reuse controls directly or indirectly. Understanding the reuse mechanism is the key, so it is not difficult to understand the upward slide. In addition, there is a method in recyclebin, setviewtypecount(). This is set for getviewtypecount () in the adapter. For each data type, setviewtypecount() will open a separate recyclebin recycling mechanism for each data type. We just need to know here. As for seeing that listview will onlayout many times in Guo Shen's blog, this is certain. Due to the problem of Android view loading mechanism, the child control needs to be re sized according to the size of the parent control, and can be displayed on the UI after multiple measurements. This is why view measures multiple times. As for the multiple layout of listview, I won't bother. In short, no matter how many measurements, listview will not execute repeated logic many times, that is, there will be no more than one data.

Here is the basic principle of listview reuse and the recycling mechanism of recyclebin. There are few code posts. They are all key codes. There is no need to study the code line by line. After all, it is still a grade far from the great God. We only need to know the execution process and principle.

2.ViewHolder

Finally, let's talk about viewholder. Many Android learners will confuse it with the reuse mechanism of listview. Here, the viewholder is also used during reuse, but it has nothing to do with the reuse mechanism.

When implementing an adapter, we usually add the viewholder. The viewholder has nothing to do with the reuse mechanism and principle. Its main purpose is to hold the reference of the control in the item, so as to reduce the times of findviewbyid(). Because the findviewbyid() method also affects efficiency, it plays this role in reuse, Reduce method execution times and increase efficiency. Here's a simple reminder. Don't confuse it.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support 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
分享
二维码
< <上一篇
下一篇>>