【问题标题】:howto render only the visible items of a listview如何仅呈现列表视图的可见项
【发布时间】:2021-12-07 09:39:48
【问题描述】:

据我了解,ListView 仅加载和呈现可见项目,但在我的情况下,它似乎总是呈现所有项目。因此,最多需要 5 秒(大约 150 个条目),直到 ListView 变得可见。

Logcat 显示:

I/OpenGLRenderer: Davey! duration=4740ms; 

如果我不加载图像,大约需要 1 秒。从我的角度来看也很慢。

将其限制为 5 个条目,列表显示速度很快...

这是我正在使用的代码。

listView = (ListView) view.findViewById(R.id.contact_list);
listView.setAdapter(new ContactAdapter(context, contacts));

ContactAdapter(Context context, List<Contact> contacts) {
    this.contactList = contacts;
    this.mInflater = LayoutInflater.from(context);
    this.context = context;
}

public View getView(final int position, View convertView, final ViewGroup parent) {
    final ViewHolder holder;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.contact_with_pic_ex, null);
        holder = new ViewHolder();
        holder.imgPicture = (ImageView) convertView.findViewById(R.id.picture);
        holder.txtName = (TextView) convertView.findViewById(R.id.name);
        holder.txtDate = (TextView) convertView.findViewById(R.id.date);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    final Contact contact = contactList.get(position);

    holder.imgPicture.setImageBitmap(getPhotoAsBitmap(contact.getID()));
    holder.txtName.setText(contact.getName());
    holder.txtDate.setText(contact.getDate());
    
    return convertView;
}

private Bitmap getPhotoAsBitmap(final int ID) {
    Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, ID);
    InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), uri, true);

    Bitmap bitmap;
    if (is == null)
        bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.contour_dark);
    else {
        bitmap = BitmapFactory.decodeStream(input);
    }
    return bitmap;
}

无论我使用雇员还是缩略图,这都需要相同的时间。

您能帮我更快地编写代码吗?

【问题讨论】:

  • 我建议您使用较新的RecyclerView 而不是ListView
  • 这里最大的问题是你没有缓存任何变量。限制为 5 并在 getView() 中放置一个日志来告诉您它被调用了多少次是一种方法。使用position 作为基础启用和禁用视图可能会有所帮助。但归根结底,为什么不直接使用RecyclerView
  • 如果RecyclerView让它跑得更快,我会试试的。

标签: android listview


【解决方案1】:

渲染很慢,因为BitmapFactory.decodeStream这条指令很慢,渲染前必须在后台线程中处理。

可能是这样的:

private static class ContactLoader extends AsyncTask<Void, Void, List<Contact>> {
    
    private final Context context;
    
    public ContactLoader(Context context) {
        this.context = context;
    }

    @Override
    protected List<Contact> doInBackground(Void... voids) {
        List<Contact> contacts = getContactListSomeWhere();
        for(Contact contact : contacts) {
            contact.setImageBitmap(getPhotoAsBitmap(contact.getID()))
        }
        return contacts;
    }

    @Override
    protected void onPostExecute(List<Contact> contacts) {
        super.onPostExecute(contacts);
        // Render data to ListView
    }

    private Bitmap getPhotoAsBitmap(final int ID) {
        Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, ID);
        InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), uri, true);

        Bitmap bitmap;
        if (is == null)
            bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.contour_dark);
        else {
            bitmap = BitmapFactory.decodeStream(input);
        }
        return bitmap;
    }
}

【讨论】:

  • 我不明白,这应该有什么帮助。 doInBackground 方法也可以串行加载图像。在 doInBackground 完成后调用 onPostExecution。它也消耗大量内存,因为所有图像都存储在联系人列表中。顺便说一下,对于 API 30+,AsycTask 已被弃用。您能否解释一下,您的解决方案应该如何提供帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-10
  • 2021-02-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多