2

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) {
        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.

Maurice
  • 6,413
  • 13
  • 51
  • 76
  • I am doing something similiar HERE http://stackoverflow.com/questions/10224233/alphabetindexer-with-custom-adapter-managed-by-loadermanager – Etienne Lawlor Apr 25 '12 at 06:12

1 Answers1

0

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

Dyonisos
  • 3,541
  • 2
  • 22
  • 25
  • I dont really understand what you want to do with this part. In my opinion this should be replaced. Have you looked at the example? It shows all the necesary methods. – Dyonisos Sep 06 '11 at 08:23
  • Hmmm... strange. I saw a difference in the performanc for me. Maybe you could also try to make the listview load the data dynamically. See: `http://p-xr.com/android-tutorial-dynamicaly-load-more-items-to-the-listview-never-ending-list/` – Dyonisos Sep 06 '11 at 08:28
  • I also had to implement a list with a huge amount of data. I created a method which collected the necesary data for one Screen in a ArrayList. As soon as the end of the list is reached the loader method is called again and adds the next screen entries to the ArrayList. If you do this in an asyncTask also the performance should increase. – Dyonisos Sep 06 '11 at 08:54
  • It's amazing how when the syncing of contacts is going on, they can seamlessly update the contacts list as well, and we are talking like 1500 contacts being deleted. – Maurice Sep 06 '11 at 09:03
  • Then maybe you have a look in the contacts source: `https://github.com/android/platform_packages_apps_contacts` and find out how. – Dyonisos Sep 06 '11 at 09:08