ListView Performance Optimization Summary

Source: Internet
Author: User

1. Android:layout_height Properties:

The layout Height property of the ListView must be set to non-wrap_content (which can be absolute values such as MATCH_PARENT/FILL_PARENT/400DP), if the ListView has a layout height of "wrap_content", Then GetView () is called repeatedly. In general, an item will be called about three times.

2, Viewholder:

Using the Viewholder inner class, define the control that needs to be shown in the item layout file as a property (in fact Viewholder is a custom model class). This makes it possible to synthesize a whole number of controls scattered in the item, which effectively avoids image dislocation.

3, Convertview:

The ListView load is an item loaded with an item, so that it inflate an item layout every time, and then Findviewbyid all the controls on that layout again. When the amount of data is large, it is unthinkable. And the use of recycle recycling can solve the problem. So be good at reusing Convertview, this can reduce the process of filling layout, reduce the number of Viewholder object instantiation. Reduce memory overhead and improve performance.

4, Convertview's Settag ():

Use the Settag () method to attach the Viewholder object as a label to the Convertview, when Convertview is reused because there are viewholder objects on it, So Convertview has several attributes in the Viewholder, thus saving the Findviewbyid () process. If an item has three controls, if there are 100 item, then in the process of loading data, it is equivalent to save hundreds of times Findviewbyid (), save the time of executing Findviewbyid (), increase the loading speed, save the cost of performance.

5. The inflate () method of the Layoutinflater object:

The inflate () method generally receives two parameters, the first parameter is the layout ID to be loaded, and the second parameter refers to the outside of the layout and then nested a layer of parent layout, if you do not need to directly pass NULL.

The inflate () method also has a method overload that receives three parameters

1. If Root is null,attachtoroot will lose its function, setting any value is meaningless. 2. If root is not set to true for Null,attachtoroot, the root layout is then nested in the outermost layer of the loaded layout file. 3. If root is not set to False for Null,attachtoroot, the root parameter loses effect. 4. If the Attachtoroot parameter is not set, the default is true if root is not the Null,attachtoroot parameter.

So when using Layoutinflater to populate layouts, be aware of the parameters of the inflate () method. If it is two arguments, the second parameter can be null, and if you use the three-parameter method, pay attention to the collocation of the parameters.

6, monitor the scrolling status of the screen change situation:

The ListView object has a Onscrolllistener listener. The second parameter of its callback method onscrollstatechanged (Abslistview view, int scrollstate) is the screen scrolling state.

Scrollstate = Scroll_state_touch_scroll (1): Indicates that scrolling is in progress. 1 when the screen scrolls and the user uses a touch or finger that is still on the screen

Scrollstate =scroll_state_fling (2): Indicates that the finger does the action of throwing (the finger leaves the screen, a hard slide, the screen produces inertia sliding).

Scrollstate =scroll_state_idle (0): Indicates that the screen is stopped. 0 when the screen stops scrolling.

In the above three screen scrolling states, if you are still in the scroll_state_fling state, the screen is still in an inertial slide state, and you can not load the picture asynchronously. This saves you the unnecessary performance overhead.


Suggestions:

1, use the ListView when the attention to use layout_height= "Match_parent".

2, if the 1th can not be avoided, you need to pay attention to the parent layout of the ListView, the parent layout should never use relativelayout, even if using framelayout or linearlayout will increase the layout level.

3, if the 1th can not be avoided, you need to be careful not to use setvisibility in GetView this will trigger the ListView onmeasure operation.

4, if the ListView has displacement, such as down to refresh, and so on, absolutely must follow the 1th to set layout_height= "Match_parent", or the frequent trigger onmeasure will lead to the interaction lag.

5, about Noscrolllistview, this layout is strictly forbidden to use, regardless of which scene, if the ScrollView must use the ListView, you can use the Simulatelistview control instead of the ListView https:// Github.com/megatronking/simulatelistview

6, due to the GridView measure mechanism and the ListView some differences, although the same will have a performance loss but not small, but it is recommended that developers follow the above points.

7. Use logic sparingly in the GetView method in adapter

8, the best possible to avoid GC

9, when the slide does not load the picture

10, the less the layout level of the item, the better

11. Try to avoid using threads in the ListView adapter

12. Set the Scrollingcache and Animatecache of the ListView to False

13. If the layout is complex, consider whether the custom layout can be implemented

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.