我有girdview
显示,图片和文字,滚动gridview
时,图片顺序发生变化,文字保持正确,但图片会发生变化。我使用Viewholder
模式m但仍然遇到同样的问题..下面是我的代码:
@Override
public View getView(int position, View v, ViewGroup parent) {
// Keeps reference to avoid future findViewById()
final ViewHolder viewHolder;
if (v == null) {
LayoutInflater li = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.row_recipe, parent, false);
viewHolder = new ViewHolder();
viewHolder.tvRecipeName = (TextView) v.findViewById(R.id.txtUserName);
viewHolder.ivRecipe = (ImageView) v.findViewById(R.id.ivRecipe);
viewHolder.tvLikes = (TextView) v.findViewById(R.id.tvLikes);
viewHolder.loader = (ProgressBar) v.findViewById(R.id.loader);
v.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) v.getTag();
}
final Recipe recipe = values.get(position);
if (recipe != null) {
viewHolder.tvRecipeName.setText(recipe.name);
viewHolder.tvLikes.setText(String.valueOf(recipe.likesCount));
// Thread to load image of the recipe
Photo mainPhoto = recipe.getMainPhoto();
if (mainPhoto != null && mainPhoto.name != null
&& !mainPhoto.name.equals("null")) {
mainPhoto.format = CompressFormat.JPEG;
mainPhoto.quality = 80;
mainPhoto.sample = true;
// Calculate inSampleSize
Display display = mActivity.getWindowManager()
.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
int reqWidth = width / 2;
double widthHeightRatio = (int) Math.ceil(height / width);
int reqHeight = (int) Math.ceil(reqWidth * widthHeightRatio);
mainPhoto.width = reqWidth;
mainPhoto.height = reqHeight;
// setup a listener to just change the one image view instead of
// calling notifyDataSetChanged which redraws the entire list
// view screen.
mainPhoto.listener = new ImageCacheListener() {
@Override
public void onSuccess(Bitmap bitmap, Photo photo) {
super.onSuccess(bitmap, photo);
viewHolder.ivRecipe.setImageBitmap(bitmap);
viewHolder.loader.setVisibility(View.INVISIBLE);
}
@Override
public void onError(Throwable throwable, Photo photo) {
super.onError(throwable, photo);
}
};
// get the friend image or the default
Bitmap recipePhoto = remoteImageCache.getImage(mainPhoto);
if (recipePhoto != null)
viewHolder.ivRecipe.setImageBitmap(recipePhoto);
} else {
viewHolder.loader.setVisibility(View.INVISIBLE);
}
}
return v;
}
static class ViewHolder {
TextView tvLikes;
ProgressBar loader;
ImageView ivRecipe;
TextView tvRecipeName;
}
答案 0 :(得分:1)
当您的位图在单独的线程中处理时,您的视图正在被回收,您需要确保最初以该过程开始的图像视图在完成时是相同的。
http://developer.android.com/training/displaying-bitmaps/process-bitmap.html
答案 1 :(得分:0)
最佳解决方案我可以建议使用Volley库(Easy和Best)。我已经在2个项目中使用过它,就像魅力一样。
http://www.androidhive.info/2014/05/android-working-with-volley-library-1/