The GetView of the book
Java code
Public View GetView (int position, View Convertview, ViewGroup parent) {
View view = new view ();
Find layouts by inflate, etc., and then set up each item displayed by Findviewbyid.
return view;
}
And in the process of the ListView sliding, it is easy to find that every time GetView is executed, the new View object will have a lot of consumption in the long run.
Especially when the item has bitmap and so on, it can even cause oom errors to cause the program to crash.
When you look at the parameters provided by GetView, you may have noticed that there is a parameter view Convertview and this convertview is actually the most important part of the theory.
When the ListView is sliding, the item is slid out of the screen and is no longer in use when Android reclaims the view of this entry
This view is the convertview here.
In the above practice, when item1 is removed from the screen we will re-new a view to the newly displayed item_new and if we use this convertview we can actually reuse it.
This eliminates the overhead of new view
Here's what happens when you use Convertview.
Java code
Public View GetView (int position, View Convertview, ViewGroup parent) {
View view = null;
if (Convertview! = null) {
view = Convertview;
Reuse a recycled view just make the changes to the content fill directly.
} else {
view = new Xxx (...);
No re-use view creates a new view as a general practice
}
return view;
}
This avoids the problem of repeatedly creating a large number of view
But it's still flawed. When the item populated in our ListView has many forms, such as the item in the microblog contains a picture of the item containing the video then the inevitable
We need to use 2 kinds of item layout way
At this point, if you simply determine if convert exists, it will cause the recovered view to not conform to the layout you currently need, and the conversion fails to exit
Here are a few other 2 ways to mention adapter:
public int Getitemviewtype (int position) {}
public int Getviewtypecount () {}
From the method name can be more obvious understand these 2 roles
A demo code is attached below
Java code
Class Myadapter extends baseadapter{
Context Mcontext;
LinearLayout linearlayout = null;
Layoutinflater Inflater;
TextView Tex;
final int view_type = 2;
Final int type_1 = 0;
final int type_2 = 1;
Public Myadapter (Context context) {
Mcontext = context;
Inflater = Layoutinflater.from (Mcontext);
}
@Override
public int GetCount () {
return Liststring.size ();
}
Each convert view calls this method to get the currently required view style
@Override
public int Getitemviewtype (int position) {
int p = position%6;
if (p = = 0)
return type_1;
else if (P < 3)
return type_2;
Else
return type_1;
}
@Override
public int Getviewtypecount () {
return 2;
}
@Override
Public Object getItem (int arg0) {
Return Liststring.get (arg0);
}
@Override
public long getitemid (int position) {
return position;
}
@Override
Public View GetView (int position, View Convertview, ViewGroup parent) {
ViewHolder1 holder1 = null;
ViewHolder2 holder2 = null;
int type = Getitemviewtype (position);
No Convertview, need to new out each control
if (Convertview = = null)
{
Determine the layout of new by the current desired style
Switch (type)
{
Case Type_1:
Convertview = Inflater.inflate (r.layout.listitem1, parent, false);
Holder1 = new ViewHolder1 ();
Holder1.textview = (TextView) Convertview.findviewbyid (R.ID.TEXTVIEW1);
Holder1.checkbox = (CheckBox) Convertview.findviewbyid (R.id.checkbox);
Convertview.settag (Holder1);
Break
Case type_2:
Convertview = Inflater.inflate (r.layout.listitem2, parent, false);
Holder2 = new ViewHolder2 ();
Holder2.textview = (TextView) Convertview.findviewbyid (R.ID.TEXTVIEW2);
Holder2.imageview = (ImageView) Convertview.findviewbyid (R.id.imageview);
Convertview.settag (HOLDER2);
Break
}
}
Else
{
Have Convertview, by style, get the layout without
Switch (type)
{
Case Type_1:
Holder1 = (viewHolder1) convertview.gettag ();
Break
Case type_2:
Holder2 = (viewHolder2) convertview.gettag ();
Break
}
Set up resources
Switch (type)
{
Case Type_1:
Holder1.textView.setText (integer.tostring (position));
Holder1.checkBox.setChecked (TRUE);
Break
Case type_2:
Holder2.textView.setText (integer.tostring (position));
Holder2.imageView.setBackgroundResource (R.drawable.icon);
Break
}
}
return convertview;
}
}
Control resources for each layout
Class viewholder1{
CheckBox checkbox;
TextView TextView;
}
Class viewholder2{
ImageView ImageView;
TextView TextView;
}
Here, for each view, a viewholder is used to control its internal sub-item
Another area to be aware of is that using the Settag and Gettag methods to bind holder to a view is also a trick.
The above basic is the main content of the following to supplement the actual operation of some tips
* If the Convertview on the type is a bit cumbersome or does not need to be so complex only rarely have a different situation then can also be obtained after the Convertview through Java
Instanceof to determine whether it can be strong if not strong turn to create a new view of the practice but in fact, this practice is not standardized, so it is recommended that the above practice
* The second one is about the ListView for solid color The item background can actually be set directly backgroundcolor instead of using the picture this part can actually have no small promotion the same
For any solid color background should try to set the RGB color instead of using a picture to do the background back
Reproduced Android leverages Convertview to optimize ListView performance