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, ...
随机推荐
- SQL server 2008定期的备份数据库及删除job
在SQL Server中出于数据安全的考虑,所以需要定期的备份数据库.而备份数据库一般又是在凌晨时间基本没有数据库操作的时候进行,所以我们不可能要求管理员 每天守到晚上1点去备份数据库.要实现数据库的 ...
- string.Format字符串格式化说明
1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) string.Format("{0:C}",0.2) 结果为:¥0.20 (英文操作系统结果:$0 ...
- Locust分布式负载测试工具入门
忽略元数据末尾 回到原数据开始处 Locust简介 Locust是一个简单易用的分布式负载测试工具,主要用来对网站进行负载压力测试. 以下是github上的仓库地址 https://github.co ...
- bash的输出多行和vim的全部选择
使用cat命令加输出符>来在bash脚本里面输出多行文本是最直观的做法. cat >out.file <<EOF start a line ... ... a line aga ...
- 响应式布局框架 Pure-CSS 5.0 示例中文版-中
8. 表单 Form 在 form 标签中添加 .pure-form 类,可生成单行表单(inline) 效果图: 代码: <form class="pure-form"&g ...
- 在sql中根据成绩显示学生排名
1.准备 create table newtable ( name ), yuwen ), shuxue ) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; , ); , ...
- RDD缓存学习
首先实现rdd缓存 准备了500M的数据 10份,每份 100万条,存在hdfs 中通过sc.textFile方法读取 val rdd1 = sc.textFile("hdfs://mini ...
- qt 多点触摸
http://www.ptrackapp.com/apclassys-notes/embedded-linux-multitouch/ Embedded Linux Multitouch with Q ...
- IOS让返回按钮显示自定义标题而不是上个ViewController的title
在开发IOS的时候发现当从一个ViewController跳转到另一个ViewController时,被跳转到的那个ViewController的返回按钮总是显示的是上个ViewController的 ...
- tp-04 框架与模板的整合