如何更新 ListView 中的单行? [英] How can I update a single row in a ListView?

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

问题描述

我有一个 ListView 显示新闻项目.它们包含图像、标题和一些文本.图像在单独的线程中加载(带有队列和所有线程),当图像被下载时,我现在调用列表适配器上的 notifyDataSetChanged() 来更新图像.这有效,但 getView() 被调用太频繁,因为 notifyDataSetChanged() 为所有可见项目调用 getView().我只想更新列表中的单个项目.我该怎么做?

I have a ListView which displays news items. They contain an image, a title and some text. The image is loaded in a separate thread (with a queue and all) and when the image is downloaded, I now call notifyDataSetChanged() on the list adapter to update the image. This works, but getView() is getting called too frequently, since notifyDataSetChanged() calls getView() for all visible items. I want to update just the single item in the list. How would I do this?

我目前的方法存在的问题是:

Problems I have with my current approach are:

  1. 滚动很慢
  2. 我在图像上有一个淡入动画,每次加载列表中的单个新图像时都会发生这种情况.

推荐答案

我找到了答案,感谢 Michelle 提供的信息.您确实可以使用 View#getChildAt(int index) 获得正确的视图.问题是它从第一个可见项目开始计数.实际上,您只能获取可见项.你用 ListView#getFirstVisiblePosition() 解决这个问题.

I found the answer, thanks to your information Michelle. You can indeed get the right view using View#getChildAt(int index). The catch is that it starts counting from the first visible item. In fact, you can only get the visible items. You solve this with ListView#getFirstVisiblePosition().

示例:

private void updateView(int index){
    View v = yourListView.getChildAt(index - 
        yourListView.getFirstVisiblePosition());

    if(v == null)
       return;

    TextView someText = (TextView) v.findViewById(R.id.sometextview);
    someText.setText("Hi! I updated you manually!");
}

这篇关于如何更新 ListView 中的单行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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