我尝试使用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 你能告诉我我哪里错了吗?提前致谢。
更新:另一个,下载时不显示进度条。