3

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

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

4

3 回答 3

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(response, true) 以反映任何已经可用的缓存数据。如果数据可用, response.getBitmap() 将是 non-null。2. 网络响应返回后,只会出现以下一种情况: - 加载图片时会调用onResponse(response, false)。或 - 如果加载图像时出错,将调用 onErrorResponse

于 2014-03-21T00:37:44.093 回答
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);
            }
        });
    }
}
于 2015-09-19T09:38:14.747 回答
1

在networkimageview之前改LinearLayoutFrameLayout改顺序进步就是这样

<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 类

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

}
于 2015-11-29T11:31:15.907 回答