Android Basics Getting Started tutorial--2.4.3 baseadapter optimization
tags (space delimited): Android Basics Getting Started Tutorial
Introduction to this section:
In the previous section we learned how to use a ListView and customize a simple baseadapter that we have from the code
You can see two more important methods: GetCount () and GetView (), how many GetView are called on the interface,
This time may see some clues, each time is a new inflate a view, all to do this XML parsing, this will
is a waste of resources, of course, the list of dozens of or hundreds of columns does not reflect any problem, but what if more or more complex layouts?
So it is important to learn the optimization of the ListView, and this section is for the optimization of the baseadapter, the optimization of the two points have, multiplexing Convertview
And the use of viewholder reuse components, not every time Findviewbyid, we specifically through the code to understand it!
1. Multiplexing Convertview:
The above also says, how many item on the interface, then the GetView method will be called How many times!
Let's take a look at the previous section of the code we wrote about the GetView () section:
@Override public View getView (int position, view Convertview, ViewGroup parent) {Convertview = Layoutinflater. from(Mcontext). Inflate(R. Layout. Item_list_animal,parent,false);ImageView Img_icon = (ImageView) convertview. Findviewbyid(R. ID. IMG_icon);TextView txt_aname = (TextView) convertview. Findviewbyid(R. ID. txt_aname);TextView txt_aspeak = (TextView) convertview. Findviewbyid(R. ID. txt_aspeak);Img_icon. Setbackgroundresource(Mdata. Get(position). Getaicon());Txt_aname. SetText(Mdata. Get(position). Getaname());Txt_aspeak. SetText(Mdata. Get(position). Getaspeak());Return Convertview;}
Yes, inflate () to load XML every time, in fact, this convertview is the system provided to us to take the view
Cache object, then sit down and judge, modify the code after the optimization:
@Override public View getView (int position, view Convertview, ViewGroup parent) {if (Convertview = = null) { Convertview = Layoutinflater. from(Mcontext). Inflate(R. Layout. Item_list_animal,parent,false);} ImageView Img_icon = (ImageView) convertview. Findviewbyid(R. ID. IMG_icon);TextView txt_aname = (TextView) convertview. Findviewbyid(R. ID. txt_aname);TextView txt_aspeak = (TextView) convertview. Findviewbyid(R. ID. txt_aspeak);Img_icon. Setbackgroundresource(Mdata. Get(position). Getaicon());Txt_aname. SetText(Mdata. Get(position). Getaname());Txt_aspeak. SetText(Mdata. Get(position). Getaspeak());Return Convertview;}
2.ViewHolder Reusing components
Hey, GetView () will be called multiple times, then Findviewbyid not have to be called multiple times, and our ListView item
are generally the same layout, we can be optimized here, we can define ourselves a viewholder class to this part
Optimize for performance! The modified code is as follows:
@Override public View getView (int position, view Convertview, ViewGroup parent) {Viewholder holder = null;if (Convertview = = null) {Convertview = Layoutinflater. from(Mcontext). Inflate(R. Layout. Item_list_animal,parent,false);Holder = new Viewholder ();Holder. IMG_icon = (ImageView) convertview. Findviewbyid(R. ID. IMG_icon);Holder. txt_aname = (TextView) convertview. Findviewbyid(R. ID. txt_aname);Holder. txt_aspeak = (TextView) convertview. Findviewbyid(R. ID. txt_aspeak);Convertview. Settag(holder); Storing holder in Convertview}else{holder = (viewholder) convertview. Gettag();} Holder. IMG_icon. Setbackgroundresource(Mdata. Get(position). Getaicon());Holder. txt_aname. SetText(Mdata. Get(position). Getaname());Holder. txt_aspeak. SetText(Mdata. Get(position). Getaspeak());Return Convertview;} static class viewholder{ImageView Img_icon;TextView Txt_aname;TextView Txt_aspeak;}
Yes it is so simple, you later Baseadapter follow this template written on the right, haha, and the other modified Viewholder
Static you can see the situation changed to other, if the entry is less, you can use static, if the item is more flowers, or not
defined as static ~
This section summarizes:
Well, the optimization about Baseadapter is probably two of the above, very simple, multiplexed convertview and custom Viewholder
Reduce Findviewbyid () calls ~ if you have other suggestions about Baseadapter optimization Welcome, thank you ~
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Basics Getting Started tutorial--2.4.3 baseadapter optimization