Listview for Android performance optimization (ViewHolder reuse mechanism)

Transferred from http://www.2cto.com/kf/201505/398415.html
I believe that everyone will use the ListView control many times, because it is indeed used a lot, but I have encountered a lot of data. Sometimes, when sliding down the ListView, it sometimes freezes, which requires us to optimize it.



ListView optimization mainly includes the following aspects:

1. ConvertView reuse

2. ViewHolder's sub-View reuse

3. Cached data reuse



1.

ConvertView reuse getView() method, this method will pass in a parameter of convertView, the View returned by this method is the View displayed by this Item. If the number of Items is large enough, and then create a View object for each Item, it will take up a lot of memory space, that is, create a View object (mInflater.inflate(R.layout.lv_item, null); Generate View from xml, This is an IO operation) is a time-consuming operation, so it will definitely affect performance. Android provides a component called Recycler (repetitive cycle), that is, when the Item of the ListView is scrolled out of the screen perspective, the View corresponding to the Item will be cached in the Recycler, and an Item will be generated accordingly, and the call will be called at this time. The convertView parameter in getView is the View of the cached Item that scrolls off the screen, so if you can reuse this convertView, it will greatly improve the performance.

So how do we reuse it? Paste code:



As shown in the figure, when this convertView does not exist, that is, when it is used for the first time, we create a View object with an item layout and assign it to convertView. When using convertView in the future, we only need to take it out from the getTag of convertView, and there is no need to do it again. The layout object of the item is created, which improves performance.

Second, use ViewHolder reuse

We all know that the operation in the getView() method is like this: first create a view object from xml (inflate operation, we use the reuse convertView method to optimize), and then go to this view to findViewById, find each The control object of the item's child View, such as: ImageView, TextView, etc. The findViewById operation here is a tree search process and a time-consuming operation, so it also needs to be optimized here, that is to use ViewHolder, put the child View control objects of each item in the Holder, when the convertView object is created for the first time , the child View control object findViewById of these items is instantiated and saved in the ViewHolder object. Then use the setTag of convertView to set the viewHolder object into the Tag. When the item of the ListView is loaded in the future, the reused ViewHolder object can be directly taken out from the Tag, and there is no need to find ViewById to find the child control object of the item. This greatly improves performance.

Paste the complete code:





Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326987069&siteId=291194637