【问题标题】:How can I download ListView HTML images asynchronously?如何异步下载 ListView HTML 图像?
【发布时间】:2012-07-29 19:11:53
【问题描述】:

作为原型,我正在尝试将android-imagedownloader demoAndroid Developers Blog 修改为使用HTML 图像标签而不是硬编码ImageViews。我已通过将ImageAdapter.getView() 替换为以下内容对其进行了修改:

public View getView(final int position, View view, final ViewGroup parent) {
   TextView textView = (TextView) view;
    if (textView == null) {
       textView = new TextView(parent.getContext());
       textView.setPadding(6, 6, 6, 6);
    }
    textView.setText(Html.fromHtml("<img src=\"" + URLS[position] + "\"/>", 
       new ImageGetter() {
       @Override public Drawable getDrawable(final String source) {
          ImageView imageView = new ImageView(parent.getContext());
          imageDownloader.download(source, imageView);
          return imageView.getDrawable();
       }
    }, null));

    return textView;
}

ListView 中没有出现任何图像,Logcat 中也没有报告错误。与ImageViews 不同,TextViews 是否需要在下载图像后以某种方式刷新?

原来的getView()是:

public View getView(int position, View view, ViewGroup parent) {
    if (view == null) {
        view = new ImageView(parent.getContext());
        view.setPadding(6, 6, 6, 6);
    }

    imageDownloader.download(URLS[position], (ImageView) view);

    return view;
}

【问题讨论】:

    标签: android html listview android-listview android-asynctask


    【解决方案1】:

    我将放弃使用 HTML TextView 呈现图像的最初想法,因为我认为将布局限制为每个 TextView 的单个图像不会有问题。对于那些仍然真的想这样做的人,this answer 应该会指导您找到正确的解决方案。您必须将TextView 传递给BitmapDownloaderTask 并在onPostExecute 中再次调用setText,但使用新下载或“解缓存”的图像。

    可能还需要根据Romain Guy's guidance 强制更新ListView 中的行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-04
      • 2020-11-20
      • 1970-01-01
      • 2015-12-04
      • 1970-01-01
      • 1970-01-01
      • 2017-12-10
      • 1970-01-01
      相关资源
      最近更新 更多