SLaks 2013-04-07 03:30 采纳率: 0%
浏览 1182

Android 中ListView 的 Alphabet Indexer滞后问题

在程序中使用CustomCursorAdapter来创建section headers。我从ListView 行中移除所有的图像,但是滚动还是相当的滞后。有谁知道用什么方法来优化吗?

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;
    }
}
  • 写回答

1条回答 默认 最新

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2022-12-20 18:44
    关注

    ChatGPT尝试为您解答,仅供参考
    有几个方法可以优化 ListView 的性能:

    • 使用复用视图:您应该使用 convertView 来复用视图,这样可以减少内存占用并减少布局加载的时间。
    • 使用视图绑定:您可以使用视图绑定来优化视图创建的性能。视图绑定是一种将数据绑定到视图的技术,可以通过在每个视图中缓存绑定数据来减少 findViewById() 调用的次数。
    • 使用常量大小的视图:您应该使用常量大小的视图,这样 ListView 就可以使用缓存来更快地滚动。
    • 减少布局嵌套:尽量减少布局嵌套,因为这会导致布局的加载时间变慢。
    • 延迟加载图像:如果您使用图像,请考虑使用图像加载库(如 Glide 或 Picasso)来延迟加载图像,这样可以减少初始加载时间。

    您可以尝试使用上述方法来优化您的 CustomCursorAdapter。

    评论

报告相同问题?

悬赏问题

  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面