Introduction and performance optimization of Android listview list control

Listview list control

1、 How listview displays data: MVC mode

m: Mode data (encapsulated by JavaBean specification)

v:view ListView

c: The adapter is responsible for displaying the data on the listview

2、 Listview most commonly used adapter

BaseAdapter、SimpleAdapter、ArrayAdapter

3、 To display data in listview

1. Create listview

2. The adapter of custom listview inherits baseadapter and overrides getcount method and getview method of baseadapter

3. Create adapter for custom listview

4. Listview setting adapter: listview.setadapter (adapter);

4、 Performance optimization of listview

1. Strange phenomenon of listview

Question:

If the height of the listview is set to wrap the content, the getview method will be called multiple times

reason:

If height is set to wrap_ "Content", in order to display all entries, it will check whether the data can be displayed on the screen for many times (one check is not accurate, and then multiple checks) and call the getview method for many times

Solution: when using listview in the future, set the height to fill the parent form Android: layout_ height=”match_ parent”

2. Avoid memory overflow Optimization: listview reuses history cache view objects

Problem: if you do not reuse the history cache view object, an error will be reported when the listview has too many entries and slides very deep.

Error message: e / dalvikvm heap (2636): out of memory on a - 294967280 byte allocation. Memory overflow

Reason: constantly creating new views consumes memory. Create a view object for each item, which will occupy a lot of memory space. Generating a view from XML is an IO operation and a time-consuming operation, so it will affect performance

Solution: you can cache the views that disappear on the screen and reuse the history cache when dragging down, so that only the number of views that can be displayed on the screen can be created. The getview method has a parameter convertview, which is a history cache view object that can be reused. 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.

Principle: Android provides a component called recycler, that is, when the listview item rolls out of the screen view, the view of the corresponding item will be cached in the recycler, and an item will be generated from the. At this time, the convertview parameter in the called getview is the view of the cached item rolled out of the screen, so if this convertview can be reused, Will greatly improve performance.

3. Item sub control display Caton Optimization: viewholder reuse mechanism

Problem: many child controls in item load slowly. 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.

Reason: the operation in the getview () method is to first create a view object from the XML (inflate operation, which is optimized by reusing the convertview method), then find the findviewbyid in this view and find the control object of the child view of each item, such as ImageView, textview, etc. The findviewbyid operation here is a tree lookup process and a time-consuming operation.

Solution: Google recommends an optimization method to deal with it, that is, rebuild an internal static class with the same number and type of member variables as those contained in the view

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, we let view = convertview and take out the holder object corresponding to the view to obtain the child controls in the view object, that is, the member variables 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 two methods are needed: settag and gettag methods in the view.

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

4. Too much network data, slow loading and optimization

Question:

If the listview displays the contents of the local list collection, the length of the list is only 100, and we can easily load the 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 using listview to display news as an example: if the network is good, the mobile phone we use may be able to load all news data at once and then display it in listview. Users may feel good. If the network is not smooth, After the user loads all the network data, the list may be 1000 news items, so the user may need to face a blank activity for several minutes, which is obviously inappropriate.

solve:

We need to load in batches, such as 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.

5. Memory overflow optimization due to excessive data loading

Question:

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.

solve:

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