In my previous blog, "Android ListView Basics", which showcases the basic use of multiple combinations of ListView and adapter, this article will show you how to handle the optimization of a ListView in a variety of ways.
In the example of the previous article, we used a picture and a text as the data for each row, and found that the effect was fully achieved, and there was no problem. But if we increase the number of item, for example, to 1000, 10000, 100000 data, this time when you open the ListView, will not help feeling "what ghost, card machine?!" Wait for a few seconds, the ListView is displayed, the user experience is very bad, especially if it is to be listed in the project, the consequences are very serious! So optimization for the ListView is critical.
how the ListView memory call mechanism worksThe main place in which the ListView consumes memory is the drawing of every listitem, as mentioned before, where each item of the ListView is drawn in the adapter GetView () method, the return value of the GetView () method is a view, This view is the views of each row, and then the ListView shows it again, so what if we didn't make any changes like we did before?
We tested the previous example by making the line number 100 and printing a log in GetView to see:
Log Print Results:
As you can see, the GetView runs 9 times when the ListView is initialized, and the interface just shows only the 9th data, that is, the GetView ()is only called when the screen is displayed, and the convertview of them are null. , and then we drag the interface down a bit,
See Logcat again:
Notice that the nineth data enters the interface from the bottom, and its getview is called again, and Convertview is still null, because the first item on the top is not completely out of the screen range, That is, the first view has not yet entered the Android recycler, and it cannot be reused , and we continue to slide down:
Logcat:
Find the 10th item of the Convertview is not empty! This is because the first item on top of the data has completely left the screen, so Android will "push" its convertview into the Recycleview , and then the 10th line appears, The Convertview parameter of the GetView method is exactly the first view stored in the recycler. Such as:
Reuse of Convertviewknowing the GetView principle of the ListView, we can begin to optimize it, which mentions that the convertview that have left the screen will be pressed into the recycler, and we can Each time the beginning of the GetView to determine whether Convertview is empty, is not empty, directly with the existing Convertview to directly operate , the code is as follows:
@Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {//TODO auto-generated method Stublog.d ("G Etview---> ", convertview+"--position: "+position); if (convertview==null) {Convertview = Inflater.inflate ( R.layout.list_item, null);} TextView Text = (TextView) Convertview.findviewbyid (r.id.list_item_text); ImageView image = (ImageView) Convertview.findviewbyid (R.id.list_item_image); Text.settext (Data.get (position). Get ("text"). toString ()); Image.setimageresource (Integer.parseint (data.get (position). Get ("image"). ToString ())); return Convertview;}
Run slide to such as:
Printing results:
Note that I circled two places, two addresses are identical! so we have successfully reused the cached view in the recycle, which effectively optimizes the memory consumption of the ListView (imagine, 100,000 views I'm going back and forth using only 10 Convertview, can I not reduce the memory overhead?) )
Use of ViewholderThe above is only the use of Convertview to optimize the effect, but note that there is still a problem, each view has a text and an image, each time to find them through the Findviewbyid, this is a huge project ... So since we can reuse the Convertview, would it be possible to cache the two child controls as well?
We can cache the child control view by customizing a viewholder to achieve better optimizations:
@Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {//TODO auto-generated method Stublog.d ("G Etview---> ", convertview+"--position: "+position); Viewholder holder = null;if (convertview==null) {Convertview = inflater.inflate (R.layout.list_item, null); holder = new Viewholder (); holder.text = (TextView) Convertview.findviewbyid (r.id.list_item_text); holder.image = (ImageView) Convertview.findviewbyid (R.id.list_item_image); Convertview.settag (holder);} Else{holder = (Viewholder) Convertview.gettag ();} Holder.text.setText (Data.get (position). Get ("text"). toString ()); Holder.image.setImageResource (Integer.parseint ( Data.get (position). Get ("image"). ToString ()); return Convertview;} public static class Viewholder{public TextView text;public ImageView image;}
Code Analysis: First customizes a Viewholder class, which is set to static, so that viewholder, no matter how many times new is pointing to the same memory space , Two member variables were added to the Viewholder class, corresponding to our child controls. Each time the GetView, the same first to determine whether the Viewholder object is empty, if it is empty, instantiate a Viewholder object, and Convertview through Findviewbyid to find the child control assigned to holder, The holder is then set on the Convertview through the Settag () method, which can be obtained by Convertview Gettag () after reuse. In fact, Viewholder is equivalent to a wrapper class of our child control, through this implementation does not have to go to findviewbyid each time to find the child control, each time to do is to reuse the previous view and control set the data , to achieve the purpose of optimization.
How to reuse a ListView multi-seed layoutThe above operation has some optimizations for the ListView, but there are still problems, if all the listitem layouts are not all the same (such as a friend circle, some are pictures, some are text, some are small videos, etc.), It can't be done with the same viewholder or Convertview, because the reuse layout doesn't have to be suitable for the new Listitem,listview, which provides two additional methods:
getitemviewtype (int position) "returns the type of the current view according to the subscript"
Getviewtypecount () "Number of types of return type"
The code is as follows:
public class Listviewadapter extends Simpleadapter{private Context context;private list<map<string,object> > data;private layoutinflater inflater;//Note that the number of integers defined here is less than the number returned by Getviewtypecount (), otherwise the error will go out of bounds private final int TYPE _1 = 0;private final int type_2 = 1;public Listviewadapter (Context context,list<map<string, object>> data, int Resource, string[] from,int[] to) {Super (context, data, resource, from, to);//TODO auto-generated constructor STUBTHIS.C Ontext = Context;this.data = Data;inflater = Layoutinflater.from (context);} Returns the size of the data, which is the number of rows in the ListView @overridepublic int GetCount () {//TODO auto-generated method Stubreturn data.size ();} Get data for a row based on subscript @overridepublic Object getItem (int position) {//TODO auto-generated method Stubreturn Data.get (position) ;} Gets the subscript for the specified item @overridepublic long getitemid (int position) {//TODO auto-generated method Stubreturn position;} @Overridepublic int Getitemviewtype (int position) {//TODO auto-generated method stub//If the current row is an even row, return type 1if (position%2==0) {return type_1;} If the current line is an odd line, the return type 2else{return type_2;}} @Overridepublic int Getviewtypecount () {//TODO auto-generated method Stubreturn 2;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {//TODO auto-generated method Stublog.d ("G Etview---> ", convertview+"--position: "+position); ViewHolder1 holder1 = null; ViewHolder2 Holder2 = Null;int type = Getitemviewtype (position), if (convertview==null) {switch (type) {case type_1: Convertview = inflater.inflate (R.layout.list_item, null); holder1 = new ViewHolder1 (); holder1.text = (TextView) Convertview.findviewbyid (r.id.list_item_text); holder1.image = (ImageView) Convertview.findviewbyid (R.id.list_item _image); Convertview.settag (holder1); Break;case Type_2:convertview = inflater.inflate (r.layout.list_item2, NULL); Holder2 = new ViewHolder2 (); holder2.text = (TextView) Convertview.findviewbyid (r.id.list_item_text2); holder2.detail = (TextView) Convertview.findviewbyid (R.ID.LIST_ITEM_DETAIL2); Convertview.settag (holder2); bReak;}} Else{switch (type) {case type_1:holder1 = (ViewHolder1) convertview.gettag (); Break;case type_2:holder2 = (ViewHolder2) Convertview.gettag (); break;}} Switch (type) {case TYPE_1:holder1.text.setText (data.get (position). Get ("text"). toString ()); Holder1.image.setImageResource (Integer.parseint (data.get (position). Get ("image"). ToString ())); Break;case type_2: Holder2.text.setText (Data.get (position). Get ("text"). toString ()); Holder2.detail.setText (Data.get (position). Get ( "Text"). toString ()); break;} return Convertview;} public static class Viewholder1{public TextView text;public ImageView image;} public static class Viewholder2{public TextView text;public TextView detail;}}
Code Analysis: Create another viewholder that is used to load and reuse another layout, in fact, on the original basis, for each operation is a layer of switch judgment, and then according to the type of types to set up two layouts respectively.
ListView Asynchronous loading disorderly sequence problemthe cause of the disorderly orderAll of the above actions belong to the synchronous loading of each row, so there is no problem. But if we are in the network asynchronously loading each row of pictures, there will be a data disturbance, the previous article has said, Android for the ListView Reycler processing, reduce memory overhead, then, whenever there is a new element into the interface will be callback GetView () method, In the GetView () method, the asynchronous request is turned on to get the picture from the network, note that the network operation is more time-consuming, that is, when we quickly slide the ListView, it is very likely that a situation, a position of the elements on the screen began to request pictures from the network, But before the picture was downloaded, it was removed from the screen . What will happen in this situation? depending on how the ListView works, the controls that are removed from the screen will soon be re-used by the newly entered screen element, and if the picture request that was just before it is responded to, the picture from the location is displayed at the current location, because, although they are in different locations, But all are shared with the same ImageView instance, so there is the case of the picture disorderly order . But not yet, the new element that enters the screen it also initiates a network request to get the picture of the current position, and when the picture is downloaded, it is set to the same imageview, so a picture appears first, then another picture.
How to solve the problem of disorderly order? Due to the length of the question attached here is a blog post of Guo Shen http://blog.csdn.net/guolin_blog/article/details/45586553 in detail to explain three ways to solve the asynchronous loading disorder.
In short, the above describes the listview of a variety of optimization, but not omnipotent , but only to play a part of the effect, real development also depends on the situation, such as if it is a multi-image, the first need to compress the picture, and do not getview to do too much time-consuming operation! I hope this article will help you understand the optimization of the ListView.
Android ListView Optimization Chapter