Android的排球 - 加载图像时,在适配器列表视图显示进度条 [英] Android Volley - Show progress bar in listview adapter when loading image

查看:248
本文介绍了Android的排球 - 加载图像时,在适配器列表视图显示进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着在列表视图项摆脱网络映像与排球。在下载图像,进度条是显示,完成后,应该躲起来。

I tried to get image from network with Volley in listview item. While downloading image, the progress bar is showing, when complete, it should be hide.

下面是我的适配器:

   public class NewsAdapter  extends CursorAdapter{
public NewsAdapter(Context context, Cursor c) {
    super(context, c, true);
}

@Override
public Cursor getItem(int position) {
    return (Cursor) super.getItem(position);
}

@Override
public void bindView(View view, final Context context, Cursor cursor) {
    final NetworkImageView imgNewsItem = (NetworkImageView) view.findViewById(R.id.imgNewsItem); 
    final ProgressBar progressNewsList = (ProgressBar) view.findViewById(R.id.progressNewsList);

    progressNewsList.setVisibility(View.VISIBLE);
    imgNewsItem.setVisibility(View.GONE);
    Ln.d("get news image: %s", cursor.getString(cursor.getColumnIndex(News.IMAGE_COLUMN)));
    ImageCacheManager.getInstance().getImageLoader().get(cursor.getString(cursor.getColumnIndex(News.IMAGE_COLUMN)), new ImageListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Ln.e(error);
            progressNewsList.setVisibility(View.GONE);
            imgNewsItem.setVisibility(View.VISIBLE);
            imgNewsItem.setImageResource(R.drawable.news_default_image);
        }

        @Override
        public void onResponse(ImageContainer response, boolean isImmediate) {
            progressNewsList.setVisibility(View.GONE);
            imgNewsItem.setVisibility(View.VISIBLE);
            imgNewsItem.setImageBitmap(response.getBitmap());
        }
    });
}

@Override
public View newView(Context arg0, Cursor arg1, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View retView = inflater.inflate(R.layout.row_view, parent, false);
    return retView;
}

}

我已经调试,发现乱射下载和缓存位图图像是好的,但图像视图不显示。

I have debugged and found that Volley download and cache image bitmap is ok, but the image view doesn't display.

我的项目布局是这样的:

My item layout look like this:

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp" >

    <com.android.volley.toolbox.NetworkImageView
        android:id="@+id/imgNewsItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        android:src="@drawable/news_default_image"
      android:visibility="gone" />

    <ProgressBar
        android:id="@+id/progressNewsList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" 
        >
    </ProgressBar>
</LinearLayout>

此示例中的ImageCacheManager: https://github.com/rdrobinson3/VolleyImageCacheExample
你能告诉我在哪里,我错了?先谢谢了。

The ImageCacheManager from this example: https://github.com/rdrobinson3/VolleyImageCacheExample Could you please tell me where I am wrong?. Thanks in advance.

更新:另外,进度条没有下载时显示

Update: another, the progress bar isn't showed when downloading.

推荐答案

您需要更改ImageListener.onResponse方法如下:

You need to change ImageListener.onResponse method to the following:

@Override
public void onResponse(ImageContainer response, boolean isImmediate) {
    Bitmap bitmap = response.getBitmap();
    if(bitmap != null){
       progressNewsList.setVisibility(View.GONE);
       imgNewsItem.setVisibility(View.VISIBLE);
       imgNewsItem.setImageBitmap(bitmap);
   }
}

以下是该ImageListener java的文档:

The following is the java doc for the ImageListener:

界面图像请求的响应处理程序。该呼叫流程
  是这样的:1。在被连接到一个请求,onResponse(响应,
  真)将被调用,以反映已经是所有缓存数据
  可用。如果数据可用,r​​esponse.getBitmap()会
  非空
。 2.一个网络响应返回之后,只有一个
  以下情况下会发生: - onResponse(响应,FALSE)将
  如果图像被加载调用。或者 - onErrorResponse将被称为如果
  有一个错误加载图像

Interface for the response handlers on image requests. The call flow is this: 1. Upon being attached to a request, onResponse(response, true) will be invoked to reflect any cached data that was already available. If the data was available, response.getBitmap() will be non-null. 2. After a network response returns, only one of the following cases will happen: - onResponse(response, false) will be called if the image was loaded. or - onErrorResponse will be called if there was an error loading the image

这篇关于Android的排球 - 加载图像时,在适配器列表视图显示进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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