Android Volley - 加载图片时在listview适配器中显示进度条

时间:2013-10-18 11:04:10

标签: android android-listview android-volley

我尝试在listview项目中使用Volley从网络获取图片。下载图像时,进度条显示,完成后应隐藏。

这是我的适配器:

   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;
}

}

我已调试并发现Volley下载和缓存图像位图正常,但图像视图无法显示。

我的项目布局如下所示:

 <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 你能告诉我我哪里错了吗?提前谢谢。

更新:另一个,下载时不会显示进度条。

3 个答案:

答案 0 :(得分:1)

您需要将ImageListener.onResponse方法更改为以下内容:

@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文档:

  

图像请求的响应处理程序的接口。呼叫流程   是这样的:1。在附加请求后,onResponse(响应,   将调用true来反映已经存在的任何缓存数据   可用。如果数据可用,则response.getBitmap()将是   非空即可。 2.网络响应返回后,只有一个   以下情况将会发生: - onResponse(response,false)将会发生   如果图像已加载则调用。或 - 如果是,则调用onErrorResponse   加载图片时出错

答案 1 :(得分:1)

如果使用方法加载Image并且ProgressBar已经以不同的方法初始化,那么在onResponse(...)上隐藏ProgressBar可能不起作用,因此您需要将ProgressBar作为参数传递。

private void loadImage(String imageUrl, final ImageView imageView, final ProgressBar progressBar) {
    //progressBar.setVisibility(View.VISIBLE);
    if (imageUrl != null) {
        mImageLoader.get(imageUrl, new ImageLoader.ImageListener() {

            @Override
            public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                if (response.getBitmap() != null) {
                    progressBar.setVisibility(View.GONE);
                    imageView.setImageBitmap(response.getBitmap());
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("onErrorResponse ", error.toString());
                progressBar.setVisibility(View.GONE);
            }
        });
    }
}

答案 2 :(得分:1)

LinearLayout更改为FrameLayout并更改顺序,以便在networkimageview之前取得进展

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

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

    <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" />


</FrameLayout>

Java代码

我在recycleview适配器中使用

@Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
      ImageLoader _ImageLoader = MySingleton.getInstance(_Context).getmImageLoader();


        String url = "...."; // your image url
  holder.ProgressBar.setVisibility(View.VISIBLE);
            _ImageLoader.get(url, new ImageLoader.ImageListener() {
                @Override
                public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                    if (response.getBitmap() != null) {
                        holder.ProgressBar.setVisibility(View.GONE);
                        holder.imageView.setImageBitmap(response.getBitmap());
                    }

                }
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });
}

MySingleton Class

public class MySingleton {

    private static MySingleton mInstance;
    private Context mContext;
    private RequestQueue mResquestQueue;
    private ImageLoader mImageLoader;

    private MySingleton(Context mCtx){
        this.mContext = mCtx;
        mResquestQueue = Volley.newRequestQueue(MyApplication.getAppContext());
        mImageLoader = new ImageLoader(mResquestQueue , new ImageLoader.ImageCache(){
            private final LruCache<String , Bitmap> cache = new LruCache<>(20);

            @Override
            public Bitmap getBitmap(String url) {
                return cache.get(url);
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {
                   cache.put(url , bitmap);

            }
        });

    }

    public static synchronized MySingleton getInstance(Context context){
        if(mInstance == null){
            mInstance = new MySingleton(context);
        }
        return mInstance;

    }

    public RequestQueue getResquestQueue(){
        if(mResquestQueue == null){
            mResquestQueue= Volley.newRequestQueue(mContext.getApplicationContext());
        }
        return mResquestQueue;
    }

    public <T> void addToRequestQueue(Request<T> request){
          mResquestQueue.add(request);
    }

    public ImageLoader getmImageLoader(){
        return mImageLoader;
    }

    public void CancelAllRequestes(){
        if(mResquestQueue != null){
            mResquestQueue.cancelAll(this);
        }
    }

}