Summary of several common optimization methods of listview in Android

Listview in Android should be one of the most commonly used components in layout, and it is very convenient to use. Here are some common optimization methods of listview:

First, we give an adapter class of listview without any optimization. We all inherit from baseadapter. Here, we use a list collection containing 100 strings as the content to be displayed by the listview project. Each item is a custom component. This component contains only one textview:

Activity:

Optimization 1:

It is also the most common optimization. In the getview method in the myadapter class, we noticed that each time a view object is required in the above writing method, it is to reinflate a view and return it, without reusing the view object. In fact, for the listview, it only needs to retain the maximum number of views that can be displayed, Other new views can use the view of the disappeared item through reuse, and the getview method also provides a parameter: convertview, which represents the view object that can be reused. Of course, this object may also be empty. When it is empty, it means that the entry view is created for the first time, so we need to inflate a view, so here, We override the getview method in the following way:

Optimization II:

The above optimization is for the reuse of view objects. After the above optimization, we don't need to regenerate every view. Let's solve the next work that needs to be done every time, that is, the search of components in the view:

In fact, findviewbyid is to find the corresponding ID in the XML file. It can be imagined that if there are many components, it will be very troublesome. What a wonderful thing if we can make the components in the view reuse with the reuse of the view.. In fact, Google also recommends an optimization method to deal with it, that is, rebuild an internal static class. The number and type of member variables are the same as those contained in the view. Our view here contains only one textview, so our static class is as follows:

So how can we use this viewholder class to achieve the reuse effect? The basic idea is that when the convertview is null, we not only re inflate a view, but also need to find findviewbyid. At the same time, we also need to obtain an object of viewholder class and assign the result of findviewbyid to the corresponding member variable in viewholder. Finally, bind the holder object with the view object.

When convertview is not null, let view = convertview and take out the holder object corresponding to the view to obtain the textview component in the view object, which is the member variable in the holder. In this way, when reusing, we don't need to find viewbyid again. We just need to find it several times at the beginning. The key here is how to bind the view to the holder object, so you need to use two methods: settag and gettag:

After the above practice, you may not feel the optimization effect. According to Google's documents, the actual optimization effect is about 5%.

Optimization III:

In the above two examples, the listview displays the contents of the local list collection, and the length of the list is only 100. We can easily load these 100 data at one time; However, in practical applications, we often need to use listview to display the content on the network. For example, we take the use of listview to display news as an example:

First, if the network is in good condition, the mobile phone we use may be able to load all the news data at once and then display it in the listview. The user may feel good. If the network is not smooth, the user loads all the network data, and the list may be 1000 news, then the user may need to face a blank activity for several minutes, This is obviously inappropriate

Second: we know that the runtime memory allocated to each application by the Android virtual machine is certain. Generally, the machine with poor performance is only 16m, which may be 64M. If we say that the total number of news we want to browse is 10000, even if the network is good, we can load it quickly, However, in most cases, memory overflow will also occur, resulting in application crash.

In order to solve the above two problems, we need to load in batches. For example, the list set of 1000 news items. We load 20 items at a time. When the user turns the page to the bottom, we add the following 20 items to the list, and then use the adapter to refresh the listview. In this way, the user only needs to wait for the transmission time of 20 items of data at a time, You don't need to wait several minutes at a time to load all the data and then display it on the listview. Secondly, this can also alleviate the crash of oom applications caused by many news loaded at one time.

In fact, batch loading can not completely solve the problem, because although we only add 20 pieces of data to the list set at a time in batches, and then refresh them to the listview, if there are 100000 pieces of data, if we successfully read the last list set, we will still accumulate a large number of pieces of data, which may still cause oom, At this time, we need to use paging. For example, we divide the 100000 pieces of data into 1000 pages, with 100 pieces of data on each page. When each page is loaded, it overwrites the contents of the list set in the previous page, and then use batch loading in each page, so that the user experience will be better.

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
分享
二维码
< <上一篇
下一篇>>