This article describes the ListView Item layout optimization techniques in Android. Share it for your reference, as follows:
I have never known that ListView has multiple layout optimization methods, which can only be achieved through hiding. I also know that the efficiency is definitely very low, but I don’t know what methods are there. I checked some information these days and then knew that Google has already thought of an optimization solution for us.
Suppose your ListView Item has three possible layout styles: for example, it is very simple to display a line of characters, which should be left, centered, and right.
At this time, 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 our data list is a list, the bean inside has a property (type) indicating which layout the item should use.@Override public int getItemViewType(int position) { return (position).type; } // Then in our getView method, it's OK@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 = xxxxxx//Like ordinary ("holdLeft"); (holdLeft); break; case TYPE_CENTER: holdCenter = new ViewHoldCenter(); convertView = xxxxxx//Like ordinary = xxxxxx//Like ordinary ("holdCenter"); (holdCenter); break; case TYPE_RIGHT: holdRight = new ViewHoldRight(); convertView = xxxxxx//Like ordinary = xxxxxx//Like ordinary ("holdRight"); (holdRight); break; default: break; } } else { switch(type) { case TYPE_LEFT: holdLeft = (ViewHoldLeft)(); ("holdLeft"); break; case TYPE_CENTER: holdCenter = (ViewHoldCenter)(); ("holdCenter"); break; case TYPE_RIGHT: holdRight = (ViewHoldRight)(); ("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; }
Google is still very human.
I hope this article will be helpful to everyone's Android programming design.