Android的Laggy AlphabetIndexer中的ListView [英] Android Laggy AlphabetIndexer in ListView

查看:130
本文介绍了Android的Laggy AlphabetIndexer中的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CustomCursorAdapter做漂亮节头。我删除所有图像在ListView行,但滚动仍然是相当laggy。任何人都知道的方式来优化呢?像上的Viber的联系人应用的滚动甚至2000个联系人真的光滑。谢谢!

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.

推荐答案

您正在创建一个列表视图项,每次getView之称。以获得性能的一个好方法是创建一个静态的viewHolder级至极重presents视图项目的数据。

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.

例如

static class ViewHolder
{
    ImageView icon;
    TextView title;
}

在getView的方法,你可以创建viewHolder的实例,那么:

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:

<一个href=\"http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html\" rel=\"nofollow\">http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html

这篇关于Android的Laggy AlphabetIndexer中的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆