Android ListView Item Layout Optimization Tips _android

Source: Internet
Author: User
Tags static class

This example describes the ListView item layout optimization techniques in Android. Share to everyone for your reference, specific as follows:

Have never known ListView have a variety of layout optimization methods, can only be realized by hiding, they also know that efficiency is certainly very low, but also do not know what method, these days and check some information, and then know that in fact Google has helped us to think about the optimization program.

Suppose your ListView item has three possible layout styles: It's simple to display a line of words, left, center, and right.

Then we can rewrite two methods in Baseadapter:

private static final int type_left = 0; 
private static final int type_center = 1; 
private static final int type_right = 2; 
@Override public int Getviewtypecount () {return 3; 
//If we have a list of data, the bean inside has an attribute (type) that indicates which layout the item should use. 
@Override public int Getitemviewtype (int position) {return list.get (position). type; //Then we can do this in our GetView method @Override public View getview (int position, View Convertview, viewgroup parent) {int type 
 = Getitemviewtype (position); 
 Viewholdleft Holdleft; 
 Viewholdcenter Holdcenter; 
 Viewholdright Holdright; 
    if (Convertview = = null) {switch (type) {case type_left:holdleft = new Viewholdleft (); 
    Convertview = xxxxxx//like ordinary Holdleft.textview = xxxxxx//like ordinary HoldLeft.textView.setText ("Holdleft"); 
    Convertview.settag (Holdleft); 
   Break 
    Case type_center:holdcenter = new Viewholdcenter (); Convertview = xxxxxx//like ordinary Holdcenter.textview = xxxxxx//as normal HoldCenter.textView.setTexT ("Holdcenter"); 
    Convertview.settag (Holdcenter); 
   Break 
    Case type_right:holdright = new Viewholdright (); 
    Convertview = xxxxxx//like ordinary Holdright.textview = xxxxxx//like ordinary HoldRight.textView.setText ("holdright"); 
    Convertview.settag (Holdright); 
   Break 
  Default:break; 
    } else {switch (type) {case type_left:holdleft = (viewholdleft) convertview.gettag (); 
    HoldLeft.textView.setText ("Holdleft"); 
   Break 
    Case type_center:holdcenter = (viewholdcenter) convertview.gettag (); 
    HoldCenter.textView.setText ("Holdcenter"); 
   Break 
    Case type_right:holdright = (viewholdright) convertview.gettag (); 
    HoldRight.textView.setText ("Holdright"); 
   Break 
  Default:break; 
} return Convertview; 
private static class Viewholdleft {private TextView TextView; 
private static class Viewholdcenter {private TextView TextView; private static class Viewholdright {private TExtview TextView; }

Gogle is still very human.

I hope this article will help you with the Android program.

Related Article

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.