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

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

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

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

  1. public class HomeWaterfallXlListAdapter extends BaseAdapter {private Bitmap defaultBm;
  2. private static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());
  3.  
  4. public HomeWaterfallXlListAdapter() {
  5. defaultBm = BitmapFactory.decodeResource(mActivity.getResources(), R.drawable.loading);
  6. }
  7.  
  8. /**刷新视图**/
  9. public void setList(List<HomeAnswersJSONResult.Answer> list) {
  10. this.answers = list;
  11. notifyDataSetInvalidated();
  12. }
  13.  
  14. /**加载更多**/
  15. public void addList(List<HomeAnswersJSONResult.Answer> list) {
  16. if (this.answers != null) {
  17. this.answers.addAll(list);
  18. notifyDataSetChanged();
  19. } else {
  20. setList(list);
  21. }
  22. }
  23.  
  24. @Override
  25. public int getCount() {
  26. return answers == null ? 0 : answers.size();
  27. }
  28.  
  29. @Override
  30. public Object getItem(int arg0) {
  31. return answers.get(arg0);
  32. }
  33.  
  34. @Override
  35. public long getItemId(int arg0) {
  36. return 0;
  37. }
  38.  
  39. @Override
  40. public View getView(int position, View convertView, ViewGroup parent) {
  41.  
  42. Answer answer = answers.get(position);
  43.  
  44. final ViewHolder holder;
  45. if (convertView == null) {
  46. convertView = LayoutInflater.from(mActivity).inflate(R.layout.item_home_infos_list, null);
  47. holder = new ViewHolder();
  48. holder.imageView = (ScaleImageView) convertView.findViewById(R.id.starImg);
  49. convertView.setTag(holder);
  50. } else {
  51.  
  52. holder = (ViewHolder) convertView.getTag();
  53. }
  54.  
  55. int w = answer.getImg().getWidth();
  56. int h = answer.getImg().getHeight();
  57. holder.imageView.setImageWidth(w);
  58. holder.imageView.setImageHeight(h);
  59. String imageUrl = answer.getImg().getUrl().toString() + "?w=" + AppActivity.screenWidth / 2;
  60. holder.imageView.setTag(imageUrl);
  61.  
  62. imageLoader.loadImage(imageUrl, new ImageSize(w, h), options, new ImageLoadingListener() {
  63.  
  64. @Override
  65. public void onLoadingStarted(String imageUri, View view) {
  66. holder.imageView.setImageBitmap(defaultBm);
  67. }
  68.  
  69. @Override
  70. public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
  71.  
  72. }
  73.  
  74. @Override
  75. public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
  76. if (loadedImage != null && imageUri.equals(holder.imageView.getTag())) {
  77. holder.imageView.setImageBitmap(loadedImage);
  78. boolean firstDisplay = !displayedImages.contains(imageUri);
  79. if (firstDisplay) {
  80. // 图片淡入效果
  81. AlphaAnimation fadeImage = new AlphaAnimation(0, 1);
  82. fadeImage.setDuration(500);
  83. fadeImage.setInterpolator(new DecelerateInterpolator());
  84. holder.imageView.startAnimation(fadeImage);
  85. displayedImages.add(imageUri);
  86. }
  87. }
  88. }
  89.  
  90. @Override
  91. public void onLoadingCancelled(String imageUri, View view) {
  92. }
  93. });
  94. convertView.setOnClickListener(new onItemClick(id));
  95.  
  96. return convertView;
  97. }
  98.  
  99. static final class ViewHolder {
  100. ScaleImageView imageView;
  101. }
  102.  
  103. private class onItemClick implements View.OnClickListener {
  104. private int id;
  105.  
  106. public onItemClick(int id) {
  107. this.id = id;
  108. }
  109.  
  110. @Override
  111. public void onClick(View arg0) {
  112. Intent intent = new Intent(mActivity, A.class);
  113. intent.putExtra("answerId", id);
  114. mActivity.startActivity(intent);
  115. }
  116. }
  117.  
  118. /**
  119. * 图片加载第一次显示监听器
  120. * @author Administrator
  121. *
  122. */
  123. private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {
  124.  
  125. static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());
  126.  
  127. @Override
  128. public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
  129. if (loadedImage != null) {
  130. ImageView imageView = (ImageView) view;
  131. // 是否第一次显示
  132. boolean firstDisplay = !displayedImages.contains(imageUri);
  133. if (firstDisplay) {
  134. // 图片淡入效果
  135. FadeInBitmapDisplayer.animate(imageView, 500);
  136. displayedImages.add(imageUri);
  137. }
  138. }
  139. }
  140. }
  141. }

ScaleImageView

  1. /**
  2. *
  3. * This view will auto determine the width or height by determining if the
  4. * height or width is set and scale the other dimension depending on the images
  5. * dimension
  6. *
  7. * This view also contains an ImageChangeListener which calls changed(boolean
  8. * isEmpty) once a change has been made to the ImageView
  9. *
  10. * @author Maurycy Wojtowicz
  11. *
  12. */
  13. public class ScaleImageView extends ImageView {
  14. private Bitmap currentBitmap;
  15. private ImageChangeListener imageChangeListener;
  16. private boolean scaleToWidth = false; // this flag determines if should
  17. // measure height manually dependent
  18. // of width
  19.  
  20. public ScaleImageView(Context context) {
  21. super(context);
  22. init();
  23. }
  24.  
  25. public ScaleImageView(Context context, AttributeSet attrs, int defStyle) {
  26. super(context, attrs, defStyle);
  27. init();
  28. }
  29.  
  30. public ScaleImageView(Context context, AttributeSet attrs) {
  31. super(context, attrs);
  32. init();
  33. }
  34.  
  35. private void init() {
  36. this.setScaleType(ScaleType.CENTER_INSIDE);
  37. }
  38.  
  39. public void recycle() {
  40. setImageBitmap(null);
  41. if ((this.currentBitmap == null) || (this.currentBitmap.isRecycled()))
  42. return;
  43. this.currentBitmap.recycle();
  44. this.currentBitmap = null;
  45. }
  46.  
  47. @Override
  48. public void setImageBitmap(Bitmap bm) {
  49. currentBitmap = bm;
  50. super.setImageBitmap(currentBitmap);
  51. if (imageChangeListener != null)
  52. imageChangeListener.changed((currentBitmap == null));
  53. }
  54.  
  55. @Override
  56. public void setImageDrawable(Drawable d) {
  57. super.setImageDrawable(d);
  58. if (imageChangeListener != null)
  59. imageChangeListener.changed((d == null));
  60. }
  61.  
  62. @Override
  63. public void setImageResource(int id) {
  64. super.setImageResource(id);
  65. }
  66.  
  67. public interface ImageChangeListener {
  68. // a callback for when a change has been made to this imageView
  69. void changed(boolean isEmpty);
  70. }
  71.  
  72. public ImageChangeListener getImageChangeListener() {
  73. return imageChangeListener;
  74. }
  75.  
  76. public void setImageChangeListener(ImageChangeListener imageChangeListener) {
  77. this.imageChangeListener = imageChangeListener;
  78. }
  79.  
  80. private int imageWidth;
  81. private int imageHeight;
  82.  
  83. public void setImageWidth(int w) {
  84. imageWidth = w;
  85. }
  86.  
  87. public void setImageHeight(int h) {
  88. imageHeight = h;
  89. }
  90.  
  91. @Override
  92. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  93.  
  94. int widthMode = MeasureSpec.getMode(widthMeasureSpec);
  95. int heightMode = MeasureSpec.getMode(heightMeasureSpec);
  96. int width = MeasureSpec.getSize(widthMeasureSpec);
  97. int height = MeasureSpec.getSize(heightMeasureSpec);
  98.  
  99. /**
  100. * if both width and height are set scale width first. modify in future
  101. * if necessary
  102. */
  103.  
  104. if (widthMode == MeasureSpec.EXACTLY || widthMode == MeasureSpec.AT_MOST) {
  105. scaleToWidth = true;
  106. } else if (heightMode == MeasureSpec.EXACTLY || heightMode == MeasureSpec.AT_MOST) {
  107. scaleToWidth = false;
  108. } else
  109. throw new IllegalStateException("width or height needs to be set to match_parent or a specific dimension");
  110.  
  111. if (imageWidth == 0) {
  112. // nothing to measure
  113. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  114. return;
  115. } else {
  116. if (scaleToWidth) {
  117. int iw = imageWidth;
  118. int ih = imageHeight;
  119. int heightC = width * ih / iw;
  120. if (height > 0)
  121. if (heightC > height) {
  122. // dont let hegiht be greater then set max
  123. heightC = height;
  124. width = heightC * iw / ih;
  125. }
  126.  
  127. this.setScaleType(ScaleType.CENTER_CROP);
  128. setMeasuredDimension(width, heightC);
  129.  
  130. } else {
  131. // need to scale to height instead
  132. int marg = 0;
  133. if (getParent() != null) {
  134. if (getParent().getParent() != null) {
  135. marg += ((RelativeLayout) getParent().getParent()).getPaddingTop();
  136. marg += ((RelativeLayout) getParent().getParent()).getPaddingBottom();
  137. }
  138. }
  139.  
  140. int iw = imageWidth;
  141. int ih = imageHeight;
  142.  
  143. width = height * iw / ih;
  144. height -= marg;
  145. setMeasuredDimension(width, height);
  146. }
  147.  
  148. }
  149. }
  150.  
  151. }

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. C# WINFORM判断程序是否运行,且只能运行一个实例(转)

    判断程序是否已经运行,使程序只能运行一个实例有很多方法,下面记录两种, 方法1:线程互斥 static class Program { private static System.Threading. ...

  2. Mysql 修改数据库,mysql修改表类型,Mysql增加表字段,Mysql删除表字段,Mysql修改字段名,Mysql修改字段排列顺序,Mysql修改表名

    对于已经创建好的表,尤其是已经有大量数据的表,如果需要对表做一些结构上的改变,我们可以先将表删除(drop),然后再按照新的表定义重建表.这样做没有问题,但是必然要做一些额外的工作,比如数据的重新加载 ...

  3. vue $http请求服务

    vue中的$http服务  需要引入一个叫vue-resource.js的文件,因为vue.js中没有$http服务.如果需要使用这个服务去百度下载vue-resource.js 然后引进项目即可. ...

  4. Redis(十四):主从复制

    当数据量变得庞大的时候,读写分离还是很有必要的.同时避免一个redis服务宕机,导致应用宕机的情况,我们启用sentinel(哨兵)服务,实现主从切换的功能. 主从复制 Redis 支持简单且易用的主 ...

  5. 权限管理系统(四):RBAC权限模型分类介绍

    RBAC是Role-BasedAccess Control的英文缩写,意思是基于角色的访问控制.RBAC认为权限授权实际上是Who.What.How的问题.在RBAC模型中,who.what.how构 ...

  6. 利用docker-maven-plugin快速交测

    目的 由开发环境交测的时候,通过docker镜像简化环境搭建及项目部署的过程. 环境描述 项目开发环境: windowns7 在windowns7中通过VMware Workstation安装Cent ...

  7. 浅谈AngularJS的$parse服务

    $parse 作用:将一个AngularJS表达式转换成一个函数 Usage$parse(expression) arguments expression:需要被编译的AngularJS语句 retu ...

  8. [svc]salt-jinja模版

    实现不同机器的差异化配置 把apache监听的端口统一改为8080 把配置文件files/httpd.conf 文件做成模版 修改lamp.sls改模版变量赋值 执行看结果: ok come on. ...

  9. 53. Reverse Words in a String【easy】

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  10. makefile之override

    override指示符 通常在执行 make 时,如果通过命令行定义了一个变量,那么它将替代在 Makefile中出现的同名变量的定义. 就是说,对于一个在 Makefile 中使用常规方式(使用&q ...