Android ListView item has multiple layouts

Source: Internet
Author: User

One of the key technologies of the Android ListView is redrawing the exploit.

Public View GetView (int position, View Convertview, ViewGroup parent) {

return null;

}

From the GetView function of Adatper, we can tell that the function provides a Convertview object, which is the key that we can reuse in a list to avoid redrawing each time getview. We usually use an item that is mostly a single layout, so we can reuse the same structure by creating a holder. But if we need to make some individual item in the list in our layout, his structure is different from the others. For example, the first item, or the last one is different from the other, what to do at this time. Many people say that it is better to judge directly by position, using different layouts in different position. But the idea is a good practice to find a lot of problems.

Google's official documentation has a clear hint about the GetView function, and what to do when it encounters a different item structure is nothing more than a detailed example. Here are some examples of the problems you encounter.

In Google Docs:

  Convertview:the old view to reuse, if possible. Note:you should check the This view is non-null and of a appropriate type before using. If It isn't possible to convert this view to display of the correct data, this method can create a new view. Heterogeneous lists can specify their number of view types, so it this view are always the right type ( seegetViewTypeCount() andgetItemViewType(int)).In other words, if you are working on Convertview, you have to specify how many layouts you have, in addition to judging whether or not he is empty, in a variety of layouts. and through Getitemviewtype () to get the view of which structure you are currently acquiring. Why do you want to do this, can not use position directly to judge, the main reason is that every time you get through the GetView Convertview you can not know this time the system re-use which layout of the view, so you are using Converview holder It is possible to get something that is not what you want holder this time, you may be able to report a null pointer exception such as errors. Well, now that we know the principle, let's take a look at concrete examples.

Example, the prototype is that the first item uses a layout layout_one, and all other item uses the layout layout_two.

Code:

Define a few constants inside the adapter

private static final int item_layout_type_count = 2;
private static final int type_one= 0;
private static final int type_ 1;

Rewrite Getviewtypecount (), Getitemviewtype ();


@Override
public int Getviewtypecount () {
return item_layout_type_count;
}

The type of position can be obtained by Getitemviewtype() when the system acquires Convertview through GetView.

@Override
public int Getitemviewtype (int position) {
If (position = = 0) {
return type_one;
} else {
return type_;
}
}


@Override
Public View GetView (int position, View Convertview, ViewGroup parent) {
Oneholder Oneholder = new Oneholder ();
Twoholder Twoholder = new Twoholder ();
int layouttype = Getitemviewtype (position);
if (Type_one = = Layouttype) {
if (null = = Convertview) {
Convertview = (linearlayout) mlayoutinflater.inflate (r.layout.one_layout, NULL);
Oneholder.onebutton = (Button) Convertview.findviewbyid (R.id.one_button);
Convertview.settag (Oneholder);
} else {
Oneholder = (Oneholder) convertview.gettag ();
}
} else if (Type_two = = Layouttype) {
if (null = = Convertview) {
Convertview = (linearlayout) mlayoutinflater.inflate (r.layout.two_layout, NULL);
Twoholder.twotext = (TextView) Convertview.findviewbyid (R.id.two_text);
Convertview.settag (Twoholder);
} else {
Twoholder = (Twoholder) convertview.gettag ();
}
}
return convertview;
}

Of course, if you simply do not use the reuse layout, using position to make a decision to choose a different layout is also possible, but if your layout is not too large, it is not too much trouble, but if your layout is slightly more complex, that is, every time you swipe the system will be redrawn, This time you will find the interface one card one card.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.