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 ().
The number of bars on the interface is displayed, the number of times the GetView () method is called, so assume that no optimizations are assumed at each invocation. Each time you use the View.inflate (...) method, you parse the XML file and display it to the interface. This is very resource-intensive: The old content will be destroyed as new content is generated. So, you can reuse the old content.
Optimization:
In the GetView () method, the system provides us with a historical cache object for the Reuse view Convertview, when the first screen is displayed, each item will create a new view object, which can be reused. It is very memory-intensive to create a view each time it is displayed, so in order to save memory, it can be reused when the Convertview is not null.
2, optimize two: Cache the item Entry reference--viewholder
Findviewbyid () This method is a performance-specific operation. Because of this method, the specified layout file is found. To continually parse each node: a layer of analytic queries from the topmost node. Find the back on one level after layer. If it is 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: When XML files are parsed. Just 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 creating a View object, reduce the number of times the layout file is converted to a View object, which is when the view object is created. Find all the children and save the child's references.
① defines the class that stores the control reference Viewholder
The Viewholder classes here need not be defined as static. Depending on the actual situation, assume that item is not very much. able to use. So at the time of initialization. Just load it once. Be able to get some optimizations slightly
It is only recommended that you do not use it if the item is too large. Because Static is a keyword in java. When you use it to modify a member variable, that variable belongs to that class. Rather than an instance of the class. So the variable is modified with static. Its life cycle is very long, assuming that it is used to refer to some resources consuming too many instances (for example, the context of the most), then try to avoid the use of.
Class viewholder{
Define the corresponding control in item
}
② Create your own defined class: Viewholder holder = null;
③ Add a child view to the holder:
When creating 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)
The child view is set to this holder through holder, which reduces the number of queries later
④ when reusing an entry in a ListView. Through View.gettag (). Convert the View object to holder, which translates to the corresponding reference, which makes it easy to save 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, it 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), which can load a lot of other data. 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