[Android] mobile guard blacklist function (listview optimization)
The previous article recorded that 100 pieces of data were displayed using listview. When dragging slowly, there will be no problem, but when dragging quickly, the application will report an anr error
Check the error log and see the message outofmemoryerror. There is not enough memory
When listview displays items, getview () method will be called for each displayed item
Converting XML files into view objects in this method is very resource consuming. Avoid frequent calls:
In the overridden getview () method, a view object convertview will be passed in
When you drag the listview upward, an item hidden above will be saved in the convertview object, which can be used as the target view object converted from XML
At this time, creating a new view object will only be created at the first screen display, and the subsequent entries will be displayed using the previously cached view object
The following calls the findviewbyid() method of the view object, which also consumes a lot of memory:
In Android layout, it is a typical tree structure. When searching for controls, you need to traverse the whole structure, which may be very time-consuming
Define an internal class viewholder
Define attributes according to business
In the getview () method, get the viewholder object
Assign the property of viewholder object to the view control object found by findviewbyid()
When the converted view object is created, the settag () method of the view object is called. Parameter: viewholder object
Use the gettag () method of the view object to get the viewholder object