Android ListView item has multiple la s

Source: Internet
Author: User

Android ListView item has multiple la s

A key technology of android listview is re-painting.

Public View getView (int position, View convertView, ViewGroup parent ){

Return null;

}

From the getview function of Adatper, we can know that the function provides a convertView object, which is the key for us to reuse in a list to avoid redrawing each getview. Most of the items in a single layout are commonly used. Therefore, we can create a holder to reuse the items in the same structure. However, if we need to first create some items in the list in our layout, the structure of the items is different from that of others. For example, if the first or last item is different from other items, what should we do at this time. Many people say that it is good to judge by position directly and use different la s for different positions. However, the idea is a good practice and many problems are discovered.

The official google documentation clearly prompts you about the getview function. When there are different item structures, what should we do? There are no detailed instances. The following is an example based on your own problems.

Google documentation:

ConvertView:The old view to reuse, if possible. note: You shoshould check that this view is non-null and of an appropriate type before using. if it is not possible to convert this view to display the correct data, this method can create a new view. heterogeneous lists can specify their number of view types, so that this View is always of the right type (seegetViewTypeCount()AndgetItemViewType(int)). That is to say, if you need to judge whether convertView is empty in addition to convertView operations, you have to use getViewTypeCount () to specify several la s for multiple latypes, and use getItemViewType () to obtain the view of the structure you currently obtain. Why can't you use position to determine this? The main reason is that every time you get convertView through getview, you cannot know which Layout view is reused by the system, in this way, you may not obtain the desired holder when using the converView holder. At this time, you may report errors such as null pointer exceptions. Now that we know the principle, let's look at the specific example.

For example, the prototype is that the first item uses a layout_one layout, and all other items use layout_two layout.

Code:

Define several constants in the adapter

Private static final int ITEM_LAYOUT_TYPE_COUNT = 2;
Private static final int TYPE_ONE = 0;
Private static final int TYPE_TWO = 1;

// Override getViewTypeCount () and getItemViewType ();


@ Override
Public int getViewTypeCount (){
Return ITEM_LAYOUT_TYPE_COUNT;
}

// When the system obtains convertView through getview, the corresponding position type can be obtained through getItemViewType.

@ Override
Public int getItemViewType (int position ){
If (position = 0 ){
Return TYPE_ONE;
} Else {
Return TYPE_TWO;
}
}


@ 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 reusable la S, you can use position to judge and select different la S. However, if your layout is not too large, it is not a big problem, however, if your layout is a little complicated, that is, the system will re-paint each time you slide. At this time, you will find that the interface is stuck and stuck.

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.