Android Laggy AlphabetIndexer in ListView
I have a CustomCursorAdapter to do the nice section headers. I removed all images from the ListView rows but the scrolling is still rather laggy. Anyone knows a way to optimize this? The scrolling on applications like Viber for Contacts is really smooth even for 2000 contacts. Thanks!
public View getView(int position, View convertView, ViewGroup parent) {
final int type = getItemViewType(position);
if (type == TYPE_HEADER) {开发者_如何学Go
if (convertView == null) {
final LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.list_header, parent,
false);
}
((TextView) convertView.findViewById(R.id.list_header_title))
.setText((String) getSections()[getSectionForPosition(position)]);
return convertView;
} else {
View v = super.getView(
position
- sectionToOffset
.get(getSectionForPosition(position)) - 1,
convertView, parent);
int contactIdCol = c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);
String contactId_text = c.getString(contactIdCol);
boolean flag = db.isRegistered(contactId_text);
ImageView iv = (ImageView) v.findViewById(R.id.typeImage);
if (flag) {
iv.setImageResource(R.drawable.rocket);
} else {
iv.setMinimumHeight(Config.getIconSize(context));
iv.setMinimumWidth(Config.getIconSize(context));
iv.setImageDrawable(null);
}
ImageView iv1 = (ImageView) v.findViewById(R.id.test);
cl.displayImage(contactId_text, iv1);
return v;
}
}
To further clarify, it's the fast scrolling that is jerky, the normal scroll seems fine.
You are creating a list view item every time "getView" is called. A good way to gain performance is to create a static "viewHolder" class wich represents the data of a view item.
e.g.
static class ViewHolder
{
ImageView icon;
TextView title;
}
in the "getView" method you can create an instance of the viewHolder then:
if (convertView == null) {
final LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.list_header, parent, false);
holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.name = (TextView) convertView.findViewById(R.id.title);
convertView.setTag(holder);
}
and then fill the data of your holder:
if (item != null) {
holder.title.setText("ItemTitle");
holder.icon.setImageResource(R.drawable.rocket);
}
return convertView;
for a detailled example see also:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html
精彩评论