android how to deal with data when listview refresh
如何解决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的更多相关文章
- android学习日记03--常用控件ListView
常用控件 8.ListView 列表视图,比如游戏的排行榜.列表数据可以根据屏幕大小自适应 列表的显示需要三个元素: a.ListVeiw:用来展示列表的View. b.适配器:用来把数据映射到Lis ...
- Android图表库MPAndroidChart(十四)——在ListView种使用相同的图表
Android图表库MPAndroidChart(十四)--在ListView种使用相同的图表 各位好久不见,最近挺忙的,所有博客更新的比较少,这里今天说个比较简单的图表,那就是在ListView中使 ...
- Android高级控件(一)——ListView绑定CheckBox实现全选,增加和删除等功能
Android高级控件(一)--ListView绑定CheckBox实现全选,增加和删除等功能 这个控件还是挺复杂的,也是项目中应该算是比较常用的了,所以写了一个小Demo来讲讲,主要是自定义adap ...
- android 实现一个简单纯文本的ListView
思维线路: 1.创建一个ListViewActivity,LinearLayout布局里写了一个ListView布局 2.创建一个TextView布局给ArrayAdapter适配器使用 3.将Tex ...
- Android高级控件(一)——ListView绑定CheckBox实现全选,添加和删除等功能
Android高级控件(一)--ListView绑定CheckBox实现全选,添加和删除等功能 这个控件还是挺复杂的.也是项目中应该算是比較经常使用的了,所以写了一个小Demo来讲讲,主要是自己定义a ...
- Android 快速开发系列 打造万能的ListView GridView 适配器
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38902805 ,本文出自[张鸿洋的博客] 1.概述 相信做Android开发的写 ...
- Android获取程序路径 (/data/data/appname)
Android获取文件夹路径 /data/data/ http://www.2cto.com/kf/201301/186614.html String printTxtPath = getApplic ...
- Android入门(五)UI-单位与尺寸、ListView
原文链接:http://www.orlion.ga/453/ 一.单位与尺寸 布局文件中一共有以下单位供选择:px,pt,dp,sp px:是像素,屏幕中可见的最小元素单位. pt:是磅,1磅等于1/ ...
- android 项目学习随笔十二(ListView加脚布局)
1.ListView加脚布局 头布局initHeaderView,在onTouchEvent事件中进行显示隐藏头布局切换 脚布局initFooterView,实现接口OnScrollListener, ...
随机推荐
- C# WINFORM判断程序是否运行,且只能运行一个实例(转)
判断程序是否已经运行,使程序只能运行一个实例有很多方法,下面记录两种, 方法1:线程互斥 static class Program { private static System.Threading. ...
- Mysql 修改数据库,mysql修改表类型,Mysql增加表字段,Mysql删除表字段,Mysql修改字段名,Mysql修改字段排列顺序,Mysql修改表名
对于已经创建好的表,尤其是已经有大量数据的表,如果需要对表做一些结构上的改变,我们可以先将表删除(drop),然后再按照新的表定义重建表.这样做没有问题,但是必然要做一些额外的工作,比如数据的重新加载 ...
- vue $http请求服务
vue中的$http服务 需要引入一个叫vue-resource.js的文件,因为vue.js中没有$http服务.如果需要使用这个服务去百度下载vue-resource.js 然后引进项目即可. ...
- Redis(十四):主从复制
当数据量变得庞大的时候,读写分离还是很有必要的.同时避免一个redis服务宕机,导致应用宕机的情况,我们启用sentinel(哨兵)服务,实现主从切换的功能. 主从复制 Redis 支持简单且易用的主 ...
- 权限管理系统(四):RBAC权限模型分类介绍
RBAC是Role-BasedAccess Control的英文缩写,意思是基于角色的访问控制.RBAC认为权限授权实际上是Who.What.How的问题.在RBAC模型中,who.what.how构 ...
- 利用docker-maven-plugin快速交测
目的 由开发环境交测的时候,通过docker镜像简化环境搭建及项目部署的过程. 环境描述 项目开发环境: windowns7 在windowns7中通过VMware Workstation安装Cent ...
- 浅谈AngularJS的$parse服务
$parse 作用:将一个AngularJS表达式转换成一个函数 Usage$parse(expression) arguments expression:需要被编译的AngularJS语句 retu ...
- [svc]salt-jinja模版
实现不同机器的差异化配置 把apache监听的端口统一改为8080 把配置文件files/httpd.conf 文件做成模版 修改lamp.sls改模版变量赋值 执行看结果: ok come on. ...
- 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 ...
- makefile之override
override指示符 通常在执行 make 时,如果通过命令行定义了一个变量,那么它将替代在 Makefile中出现的同名变量的定义. 就是说,对于一个在 Makefile 中使用常规方式(使用&q ...