Android Small white growth path-listview optimization proposal

Source: Internet
Author: User

In Android development, the ListView is a very important component, it is in the form of a list of data based on the long adaptive presentation of the content, the user can freely define the layout of each column of the ListView, but when the ListView has a large amount of data to load, will occupy a lot of memory, Affect performance.

The focus of this article is to describe how to optimize the ListView in the following ways.

1, Convertview Reuse

Use good convertview to reuse View, avoid every getView () new. The core principle of the ListView is to reuse the view, if the reuse view does not change the width, reuse view can reduce the memory allocation/recycling caused by the redistribution of the cache;

The Android:layout_height property value of the ListView is set to "Fill_parent" or "wrap_content", but the convertview mechanism is the same

If set to Fill_parent: The Convertview of item displayed on the screen is empty, the convetview of the newly generated item is not empty

If set to Wrap_content: Only the Convertview of the first item is NULL, the other is not empty

Summarize:

At the initial display, each time an item is displayed, the GetView method is called once but the Covertview is empty each time it is called (because there is no old view), when the display is finished. If the screen moves and causes some item (or view) to run outside the screen, if there is a new item that needs to be generated, then the Convertview parameter in the GetView method that is called when the item is displayed is not NULL. Instead of the view (old view) that moved out of the screen, all we have to do is populate the recycled view with the item that needs to be displayed, and finally note that Convertview is not just the initial display of the item, There are those item that have already started to move into the screen but have not yet been recovered by the view.

2, Viewholder optimization

The reason for using Viewholder is that the Findviewbyid method is time consuming, and if the number of controls is too large, it can seriously affect performance, and the use of Viewholder is primarily to save this time. Direct access to view via Settag,gettag

Summarize:

The Settag and Gettag methods of view are very simple, when actually writing code, a view is not just to display some strings, pictures, and sometimes we need them to carry some other data so that we can recognize the view or other operations. So the Android designers created the Settag (Object) method to hold some data and view bindings, and we can understand that this is the label of the view as well as the view as a container to hold some data. We can also use the Gettag () method to extract the data.

Here Settag and Gettag people should have understood. Returning to the above topic, we bind the data we want to display on Convertview by Convertview's Settag method and Gettag method. If Convertview is the first to show we create a new holder object with which to bind, and return it at the end through return Convertview, to display, if Convertview is recycled then we don't have to create a new holder object, Just need to get the original bound holder out and add the new data.


3, Picture loading optimization

If the ListView needs to load the display network picture, we try not to load the picture when the ListView is sliding, which will cause the ListView to become stuck, so we need to listen to the ListView state in the listener if the ListView is sliding (Scroll_ State_touch_scroll) or be scroll_state_fling, stop loading the picture, or start loading the picture if it is not sliding (scroll_state_idle).

4, Onclicklistener processing

When the ListView item has such as button these sub-view, need to set Onclicklistener, the usual way is to set in the GetView method, such as

Holder.img.setonClickListener (new Onclicklistenr) ...

However, it is inefficient to set a new onclick event each time the GetView is called. Efficient notation you can set a position directly in Viewholder, and then Viewholder implements ONCLICKLISTENR

Add: When the ListView ListItem contains a child control such as a button checkbox, the child controls will grab the focus and the simplest and most effective solution is to set the properties in the ListView item layout file root element of Android: Descendantfocusability= "Blocksdescendants"


5. Reduce the layout level of item view

This is all layout must follow, the layout level too deep will directly lead to view measurement and painting waste a lot of time

6, adapter in the GetView method as little as possible to use logic

Do not make overly complex logic in the GetView method, you can find a way to get away from somewhere else.

7, adapter in the GetView method as little as possible time-consuming operation

8, adapter in the GetView method to avoid creating a large number of objects

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

These two properties, which are turned on by default, consume a lot of memory and therefore frequently call the GC, and we can turn it off manually (as appropriate)

Other

1, take advantage of the view type, for example, there are several types of Item in your ListView, you need to create a different View for each type, which is advantageous to the collection of the ListView, of course, the type can not be too many

2, the use of custom view, custom view can effectively reduce the level of Layout, and the drawing process can be very good control;

3, as far as possible to ensure that the Adapter Hasstableids () return True, so in notifydatasetchanged (), if the ID is unchanged, the ListView will not redraw the view, to achieve the purpose of optimization;

4, use Recycleview instead. The ListView is notifydatasetchanged () every time the data is updated, and some are too violent. Recycleview has been greatly improved in terms of performance and customization, and is recommended.

This article is from the "Learning to change the Destiny" blog, please make sure to keep this source http://itzhongxin.blog.51cto.com/12734415/1913607

Android Small white growth path-listview optimization proposal

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.