Imageloader, listview.notifyDataSetChanged刷新问题,图片闪烁


转载请注明出处:http://blog.csdn.net/harryweasley/article/details/49678739


我在项目中,用了ImageLoader来显示图片,这样引出一个问题,偶尔在我调用notifyDataSetChanged这个方法时候,图片全部闪烁一下,原因是图片全部重新加载了一次,导致闪烁。


下面是我之前的getView方法中。下面的ImageLoader直接调用,造成每次notifyDataSetChanged,重新调用了ImageLoader方法。

@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.item_app_wall2, parent, false);
			holder = new ViewHolder();
			holder.icon = (ImageView) convertView.findViewById(R.id.app_icon);
			holder.name = (MarqueeText) convertView.findViewById(R.id.app_name);
			// holder.grade = (ImageView)
			// convertView.findViewById(R.id.grade);
			holder.size = (TextView) convertView.findViewById(R.id.app_size);
			holder.count = (TextView) convertView.findViewById(R.id.app_count);
			holder.ratingBar = (RatingBar) convertView.findViewById(R.id.grade);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		ImageLoader.getInstance().displayImage(searchAppList.get(position).getHttpIco(), holder.icon,
				DisplayImageOptionUtil.getInstance().getOptions());
		

		holder.name.setText(searchAppList.get(position).getName().trim());
		holder.size.setText(CommonUtil.format(searchAppList.get(position).getApkSize() / (float) 1024) + "M");
		AppStatistics statistics = searchAppList.get(position).getAppStatistics();
		if (statistics != null) {

			holder.count.setText(statistics.getDownloadCount() + "次");
		}
		holder.ratingBar.setRating(searchAppList.get(position).getAveccore() / 2f);

		return convertView;
	}



下面我做出了修改,如果当前图片是之前的图片,则不会调用ImageLoader,否则调用ImageLoader。



public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.item_app_wall2, parent, false);
			holder = new ViewHolder();
			holder.icon = (ImageView) convertView.findViewById(R.id.app_icon);
			holder.name = (MarqueeText) convertView.findViewById(R.id.app_name);
			// holder.grade = (ImageView)
			// convertView.findViewById(R.id.grade);
			holder.size = (TextView) convertView.findViewById(R.id.app_size);
			holder.count = (TextView) convertView.findViewById(R.id.app_count);
			holder.ratingBar = (RatingBar) convertView.findViewById(R.id.grade);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		
		if (searchAppList.get(position).getHttpIco().equals(holder.icon.getTag())) {

		} else {
			// 如果不相同,就加载。现在在这里来改变闪烁的情况
			ImageLoader.getInstance().displayImage(searchAppList.get(position).getHttpIco(), holder.icon,
					DisplayImageOptionUtil.getInstance().getOptions());
			holder.icon.setTag(searchAppList.get(position).getHttpIco());
		}

		holder.name.setText(searchAppList.get(position).getName().trim());
		holder.size.setText(CommonUtil.format(searchAppList.get(position).getApkSize() / (float)1024) + "M");
		AppStatistics statistics = searchAppList.get(position).getAppStatistics();
		if (statistics != null) {

			holder.count.setText(statistics.getDownloadCount() + "次");
		}
		holder.ratingBar.setRating(searchAppList.get(position).getAveccore() / 2f);
		return convertView;
	}

通过ImageView.getTag和setTag方法来,判定当前图片是否是之前的,如果是之前,则不会刷新图片,如果不是,再刷新。



你可能感兴趣的:(Imageloader, listview.notifyDataSetChanged刷新问题,图片闪烁)