我有一个ListView,我想在其中显示一些图像。显示一些图像我有网址数组。我使用凌空库下载图像。但问题是,当我第一次运行应用程序时,它只显示我的图像。但当我再次访问屏幕时,它会显示所有图像。我无法认识到这个问题,请帮帮我。
适配器
public CategoryListAdapter(Context context, ArrayList<Category> categories) {
mContext = context;
mCategories = categories;
mImageLoader = VolleySingleton.getInstance().getImageLoader();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.category_list_item, parent, false);
mViewHolder = new ViewHolder();
mViewHolder.mCategoryLogo = (ImageView) convertView.findViewById(R.id.category_image);
mViewHolder.mCategoryName = (TextView) convertView.findViewById(R.id.category_name);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (ViewHolder) convertView.getTag();
}
mViewHolder.mCategoryName.setText(mCategories.get(position).getCategory_name());
if (TextUtils.isEmpty(mCategories.get(position).getCategory_logo())) {
mViewHolder.mCategoryLogo.setImageResource(R.drawable.ic_launcher);
} else {
mImageLoader.get(mCategories.get(position).getCategory_logo(),this);
}
return convertView;
}
@Override
public void onErrorResponse(VolleyError error) {
mViewHolder.mCategoryLogo.setImageResource(R.drawable.ic_launcher);
}
@Override
public void onResponse(ImageContainer response, boolean isImmediate) {
mViewHolder.mCategoryLogo.setImageBitmap(response.getBitmap());
}
答案 0 :(得分:0)
onResponse
将被异步调用。这意味着mViewHolder
可能会引用与当前图像下载无关的其他持有者。
我建议您使用NetworkImageView
,这是Volley库的一部分。
布局:
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/category_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
在适配器中:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.category_list_item, parent, false);
mViewHolder = new ViewHolder();
mViewHolder.mCategoryLogo = (NetworkImageView) convertView.findViewById(R.id.category_image);
mViewHolder.mCategoryName = (TextView) convertView.findViewById(R.id.category_name);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (ViewHolder) convertView.getTag();
}
mViewHolder.mCategoryName.setText(mCategories.get(position).getCategory_name());
if (TextUtils.isEmpty(mCategories.get(position).getCategory_logo())) {
mViewHolder.mCategoryLogo.setImageResource(R.drawable.ic_launcher);
} else {
mViewHolder.mCategoryLogo.setImageUrl(mCategories.get(position).getCategory_logo(),mImageLoader);
}
return convertView;
}