如何解决listview数据刷新,下拉刷新,上拉加载更多时,图片不闪烁。

在Activity的onResume()方法中将adaper和listView重新再绑定一次。

listView.setAdapter(adapter);
adapter.notifyDataSetChanged();

http://www.eoeandroid.com/forum.php?mod=viewthread&tid=541113&extra=&ordertype=1

public class HomeWaterfallXlListAdapter extends BaseAdapter {private Bitmap defaultBm;
private static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>()); public HomeWaterfallXlListAdapter() {
defaultBm = BitmapFactory.decodeResource(mActivity.getResources(), R.drawable.loading);
} /**刷新视图**/
public void setList(List<HomeAnswersJSONResult.Answer> list) {
this.answers = list;
notifyDataSetInvalidated();
} /**加载更多**/
public void addList(List<HomeAnswersJSONResult.Answer> list) {
if (this.answers != null) {
this.answers.addAll(list);
notifyDataSetChanged();
} else {
setList(list);
}
} @Override
public int getCount() {
return answers == null ? 0 : answers.size();
} @Override
public Object getItem(int arg0) {
return answers.get(arg0);
} @Override
public long getItemId(int arg0) {
return 0;
} @Override
public View getView(int position, View convertView, ViewGroup parent) { Answer answer = answers.get(position); final ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(mActivity).inflate(R.layout.item_home_infos_list, null);
holder = new ViewHolder();
holder.imageView = (ScaleImageView) convertView.findViewById(R.id.starImg);
convertView.setTag(holder);
} else { holder = (ViewHolder) convertView.getTag();
} int w = answer.getImg().getWidth();
int h = answer.getImg().getHeight();
holder.imageView.setImageWidth(w);
holder.imageView.setImageHeight(h);
String imageUrl = answer.getImg().getUrl().toString() + "?w=" + AppActivity.screenWidth / 2;
holder.imageView.setTag(imageUrl); imageLoader.loadImage(imageUrl, new ImageSize(w, h), options, new ImageLoadingListener() { @Override
public void onLoadingStarted(String imageUri, View view) {
holder.imageView.setImageBitmap(defaultBm);
} @Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) { } @Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
if (loadedImage != null && imageUri.equals(holder.imageView.getTag())) {
holder.imageView.setImageBitmap(loadedImage);
boolean firstDisplay = !displayedImages.contains(imageUri);
if (firstDisplay) {
// 图片淡入效果
AlphaAnimation fadeImage = new AlphaAnimation(0, 1);
fadeImage.setDuration(500);
fadeImage.setInterpolator(new DecelerateInterpolator());
holder.imageView.startAnimation(fadeImage);
displayedImages.add(imageUri);
}
}
} @Override
public void onLoadingCancelled(String imageUri, View view) {
}
});
convertView.setOnClickListener(new onItemClick(id)); return convertView;
} static final class ViewHolder {
ScaleImageView imageView;
} private class onItemClick implements View.OnClickListener {
private int id; public onItemClick(int id) {
this.id = id;
} @Override
public void onClick(View arg0) {
Intent intent = new Intent(mActivity, A.class);
intent.putExtra("answerId", id);
mActivity.startActivity(intent);
}
} /**
* 图片加载第一次显示监听器
* @author Administrator
*
*/
private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener { static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>()); @Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
if (loadedImage != null) {
ImageView imageView = (ImageView) view;
// 是否第一次显示
boolean firstDisplay = !displayedImages.contains(imageUri);
if (firstDisplay) {
// 图片淡入效果
FadeInBitmapDisplayer.animate(imageView, 500);
displayedImages.add(imageUri);
}
}
}
}
}

ScaleImageView

/**
*
* This view will auto determine the width or height by determining if the
* height or width is set and scale the other dimension depending on the images
* dimension
*
* This view also contains an ImageChangeListener which calls changed(boolean
* isEmpty) once a change has been made to the ImageView
*
* @author Maurycy Wojtowicz
*
*/
public class ScaleImageView extends ImageView {
private Bitmap currentBitmap;
private ImageChangeListener imageChangeListener;
private boolean scaleToWidth = false; // this flag determines if should
// measure height manually dependent
// of width public ScaleImageView(Context context) {
super(context);
init();
} public ScaleImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
} public ScaleImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
} private void init() {
this.setScaleType(ScaleType.CENTER_INSIDE);
} public void recycle() {
setImageBitmap(null);
if ((this.currentBitmap == null) || (this.currentBitmap.isRecycled()))
return;
this.currentBitmap.recycle();
this.currentBitmap = null;
} @Override
public void setImageBitmap(Bitmap bm) {
currentBitmap = bm;
super.setImageBitmap(currentBitmap);
if (imageChangeListener != null)
imageChangeListener.changed((currentBitmap == null));
} @Override
public void setImageDrawable(Drawable d) {
super.setImageDrawable(d);
if (imageChangeListener != null)
imageChangeListener.changed((d == null));
} @Override
public void setImageResource(int id) {
super.setImageResource(id);
} public interface ImageChangeListener {
// a callback for when a change has been made to this imageView
void changed(boolean isEmpty);
} public ImageChangeListener getImageChangeListener() {
return imageChangeListener;
} public void setImageChangeListener(ImageChangeListener imageChangeListener) {
this.imageChangeListener = imageChangeListener;
} private int imageWidth;
private int imageHeight; public void setImageWidth(int w) {
imageWidth = w;
} public void setImageHeight(int h) {
imageHeight = h;
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec); /**
* if both width and height are set scale width first. modify in future
* if necessary
*/ if (widthMode == MeasureSpec.EXACTLY || widthMode == MeasureSpec.AT_MOST) {
scaleToWidth = true;
} else if (heightMode == MeasureSpec.EXACTLY || heightMode == MeasureSpec.AT_MOST) {
scaleToWidth = false;
} else
throw new IllegalStateException("width or height needs to be set to match_parent or a specific dimension"); if (imageWidth == 0) {
// nothing to measure
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
return;
} else {
if (scaleToWidth) {
int iw = imageWidth;
int ih = imageHeight;
int heightC = width * ih / iw;
if (height > 0)
if (heightC > height) {
// dont let hegiht be greater then set max
heightC = height;
width = heightC * iw / ih;
} this.setScaleType(ScaleType.CENTER_CROP);
setMeasuredDimension(width, heightC); } else {
// need to scale to height instead
int marg = 0;
if (getParent() != null) {
if (getParent().getParent() != null) {
marg += ((RelativeLayout) getParent().getParent()).getPaddingTop();
marg += ((RelativeLayout) getParent().getParent()).getPaddingBottom();
}
} int iw = imageWidth;
int ih = imageHeight; width = height * iw / ih;
height -= marg;
setMeasuredDimension(width, height);
} }
} }

android how to deal with data when listview refresh的更多相关文章

  1. android学习日记03--常用控件ListView

    常用控件 8.ListView 列表视图,比如游戏的排行榜.列表数据可以根据屏幕大小自适应 列表的显示需要三个元素: a.ListVeiw:用来展示列表的View. b.适配器:用来把数据映射到Lis ...

  2. Android图表库MPAndroidChart(十四)——在ListView种使用相同的图表

    Android图表库MPAndroidChart(十四)--在ListView种使用相同的图表 各位好久不见,最近挺忙的,所有博客更新的比较少,这里今天说个比较简单的图表,那就是在ListView中使 ...

  3. Android高级控件(一)——ListView绑定CheckBox实现全选,增加和删除等功能

    Android高级控件(一)--ListView绑定CheckBox实现全选,增加和删除等功能 这个控件还是挺复杂的,也是项目中应该算是比较常用的了,所以写了一个小Demo来讲讲,主要是自定义adap ...

  4. android 实现一个简单纯文本的ListView

    思维线路: 1.创建一个ListViewActivity,LinearLayout布局里写了一个ListView布局 2.创建一个TextView布局给ArrayAdapter适配器使用 3.将Tex ...

  5. Android高级控件(一)——ListView绑定CheckBox实现全选,添加和删除等功能

    Android高级控件(一)--ListView绑定CheckBox实现全选,添加和删除等功能 这个控件还是挺复杂的.也是项目中应该算是比較经常使用的了,所以写了一个小Demo来讲讲,主要是自己定义a ...

  6. Android 快速开发系列 打造万能的ListView GridView 适配器

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38902805 ,本文出自[张鸿洋的博客] 1.概述 相信做Android开发的写 ...

  7. Android获取程序路径 (/data/data/appname)

    Android获取文件夹路径 /data/data/ http://www.2cto.com/kf/201301/186614.html String printTxtPath = getApplic ...

  8. Android入门(五)UI-单位与尺寸、ListView

    原文链接:http://www.orlion.ga/453/ 一.单位与尺寸 布局文件中一共有以下单位供选择:px,pt,dp,sp px:是像素,屏幕中可见的最小元素单位. pt:是磅,1磅等于1/ ...

  9. android 项目学习随笔十二(ListView加脚布局)

    1.ListView加脚布局 头布局initHeaderView,在onTouchEvent事件中进行显示隐藏头布局切换 脚布局initFooterView,实现接口OnScrollListener, ...

随机推荐

  1. 《转》ceilometer的数据採集机制入门

    问题导读 1.ceilometer负责什么事情? 2.ceilometer 有哪些概念? 3.ceilometer 怎样採集hardware? 附上openstack 官网API   http://d ...

  2. git配置文件读取顺序

    作者:zhanhailiang 日期:2014-11-03 git包括三个配置文件: /etc/gitconfig 文件:系统中对全部用户都普遍适用的配置. 若使用git config 时用' –sy ...

  3. Web Socket rfc6455 握手 (C++)

    std::string data((const char*)buf->data(),bytes_transferred); recycle_buffer(buf); std::string ke ...

  4. [na]office 2010 2013卸载工具

    http://www.ithome.com/html/soft/32777.htm Office 2003 || Office 2007 || Office 2010.

  5. android:Notification实现状态栏的通知

    在使用手机时,当有未接来电或者新短消息时,手机会给出响应的提示信息,这些提示信息一般会显示到手机屏幕的状态栏上. Android也提供了用于处理这些信息的类,它们是Notification和Notif ...

  6. Ubuntu/Debian下编译PC版的ffmpeg

    1.安装git: 在命令行下执行 sudo apt-get install git-core 2.下载最新版本的ffmpeg: git clone git://source.ffmpeg.org/ff ...

  7. C语言 · 大数乘法

    #include<stdio.h> #include<string.h> ]; void mult(char a[],char b[]) { ,alen,blen,sum=,r ...

  8. Github优秀开源项目

    王潜升 https://github.com/code4craft/webmagic 一个爬虫框架,除了不会反爬虫外(当然可以自己加)其他都很牛逼.这个项目更新还是很快的.   ansi分词  htt ...

  9. 关于JQueryMobile中Slider的一点研究

    滑动条 Slider                 给input的设置一个新的HTML5属性为type="range",可以给页面添加滑动条组件,可以指定它的value值(当前值 ...

  10. python中的字典 和 集合

    python中字典是一种key-value的数据类型 字典的特性: 1.无序的 2.key必须的唯一的,so,字典天生去重 语法: 增加 修改 删除 查找 多级字典嵌套及操作 字典的其他用法 #set ...