我使用universal-image-loader显示gridview,并且每个网格项包括ImageView和LoadingLinearProgress,随着时间的推移,我需要更新LoadingLinearProgress(underLineProgress)设置百分比值,因此接收百分比我调用notifyDataSetChanged();每次通知(每次调用loadImage)时,gridView的图像都会振动
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.my_library_grid_item, parent, false);
holder.ivCover = (LinearLayout) convertView.findViewById(R.id.iv_cover);
holder.underLineProgress = (LoadingLinearProgress) convertView.findViewById(R.id.underline_progress);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
loadAndDisplayImage(fileName,holder.ivCover);
holder.underLineProgress.setPercent(getItem(position).getPercent);
String firstPageUrl = magazineList.get(position).getFirtPage();
loadAndDisplayImage(firstPageUrl, holder.ivCover, holder.libraryBackground);
return convertView;
}
private void loadAndDisplayImage(String fileName, ImageView imageView) {
imageLoader.displayImage(fileName, imageView, getStoringUILOptions(), new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
}
});
}
答案 0 :(得分:1)
尝试这种方式:向ViewHolder
类添加一个变量,该变量将包含magazineList.get(position)
(或getItem(position)
带来的对象中的一些信息,具体取决于网格的工作方式)。在加载图像之前,检查存储在持有者中的变量是否与当前项目中的变量具有相同的值。如果它不同,则表示视图正在被回收,否则表示已调用notifyDataSetChanged()
,在这种情况下,您不会调用loadAndDisplayImage
。
如果视图正在被回收,请将持有者的变量值设置为当前项的值。
代码示例:
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.my_library_grid_item, parent, false);
holder.ivCover = (LinearLayout) convertView.findViewById(R.id.iv_cover);
holder.underLineProgress = (LoadingLinearProgress) convertView.findViewById(R.id.underline_progress);
holder.currentMagazine = "NONE"; // to avoid checking for null every time
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.underLineProgress.setPercent(getItem(position).getPercent);
// I don't know what the above line is for, so I put it here. I guess it's
// the one you have to run every time
if(holder.currentMagazine.equals(getItem(position).title)) {// for example
loadAndDisplayImage(fileName, holder.ivCover);
String firstPageUrl = magazineList.get(position).getFirtPage();
loadAndDisplayImage(firstPageUrl, holder.ivCover, holder.libraryBackground);
holder.currentMagazine = getItem(position).title;
}
return convertView;
}
private static class ViewHolder {
String currentMagazine;
ImageView ivCover;
LoadingLinearProgress underLineProgress;
}
如果项目的图像有可能发生变化而你必须在调用notifyDataSetChanged()
时加载新图像,你必须想出一些东西才能知道图像是否与以前相同或不,并检查它。
来源:我也在我的应用程序中这样做。我通过阅读你的问题注意到了这个问题,否则每次都会让我的图像重新加载,谢谢。