First, reuse Convertview, reduce the number of Findviewbyid
1, optimization one: Multiplexing Convertview
The Android system itself has considered the ListView optimization problem. In the adapter class of the replication, the more important two methods are GetCount () and GetView (). How many bars are displayed on the interface. How many times the GetView () method is called, so assume that no optimizations are assumed at each invocation. Every time you use the View.inflate (...) method, you parse the XML file and display it to the interface, which is very resource-intensive: The old content is destroyed because of the new content, so. The ability to reuse old content.
Optimization:
In the GetView () method. The system provides us with a historical cache object Convertview for the reuse view. When the first screen is displayed. Each item creates a new view object that can be reused, assuming that one view is created each time it is displayed. is very memory-intensive. So in order to save memory. To be able to do this when the Convertview is not null. To reuse it
2, optimize two: Cache the item Entry reference--viewholder
Findviewbyid () This method is a cost-effective operation, because this method to find the specified layout file, to continue to parse each node: from the top node to a layer of analytic query, found after a layer of return, if not found on the left. It will then parse to the right. and make the corresponding query. Until you find the location (). Therefore, the Findviewbyid can be optimized for processing, it should be noted that:
"" Feature: XML file is parsed, only to be created, the child's ID will not change. According to this characteristic. The ability to deposit a child's ID into a specified set allows the corresponding element to be taken out of the collection at a time.
Optimization:
When you create a View object. Reduces the number of times a layout file is converted to a View object, that is, when you create a view object, all children are found, and the child's references are saved.
① defines the class that stores the control reference Viewholder
The Viewholder class here need not be defined as static, depending on the actual situation, assuming that the item is not very much, can be used, so at the time of initialization. Just load it once. Be able to get some optimizations slightly
Just assume that the item is too much. It is not recommended. Since Static is a keyword in Java, when it is used to decorate a member variable, that variable belongs to that class, not an instance of that class. So the variable is modified with static. Its life cycle is very long. Consider using it to refer to instances where resources are consuming too much (for example, the context is the most common). You should try to avoid using it at this time.
Class viewholder{
Define the corresponding control in item
}
② Create your own defined class: Viewholder holder = null;
③ Add a child view to the holder:
When you create a new ListView. Create a new Viewholder. Find all the children and save the child's references.
Setting references to view with View.settag (holder)
Through holder. Set the child view to this holder. Thus reducing the number of queries later
④ when reusing an entry in a ListView. Convert the View object to holder with View.gettag (). It is converted to the corresponding reference, which makes it easy to deposit the collection at the next use.
Get references by View.gettag (holder) (must be a strong turn)
Second, the data in the ListView batch and page loading:
Requirements: The ListView has 10,000 data, how to display it, assuming that 100,000 data is loaded into memory, which consumes memory very much.
The workaround:
Optimize query data: First get a few data to display on the interface
Batch processing---à optimized user experience
Paging---à optimized memory space
Description
General data is obtained from the database, the implementation of partial (paging) loading data, it is necessary in the corresponding DAO in the corresponding batch (paging) method of obtaining data. such as Findpartdatas ()
1. Prepare the data:
How to include partial loading data in DAO: Findpartdatas ()
At the time of fitting the data, we first load the first batch of data and need to load the second batch to set up a monitoring test when loading the second batch
2. Set the scroll listener for the ListView: Setonscrolllistener (New onscrolllistener{...})
①, there are two methods in the listener: The method of changing the scrolling state (onscrollstatechanged) and the method called when the ListView is scrolled (onscroll)
②, there are three states in the method of changing the rolling state:
The finger presses the moving state: scroll_state_touch_scroll://Touch Swipe
Inertial scrolling (gliding (flgin) state): scroll_state_fling://Glide
Delicate state: scroll_state_idle://Compact
3, the different status of processing:
Load data in batches, just care about the smart state: care about the last visible entry, assuming that the last visible entry is the last one in the data adapter (collection). A lot of other data can be loaded at this time. At each load, the number of scrolls is calculated, when the number of scrolls is greater than or equal to the total quantity. Be able to prompt the user without a lot of other data.
Optimizations for Android ListView