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, ...
随机推荐
- 11个常用的Linux命令
Linux命令行吸引了大多数Linux爱好者.一个正常的Linux用户一般掌握大约50-60个命令来处理每日的任务.今天为你解释下面几个命令:sudo.python.mtr.Ctrl+x+e.nl.s ...
- Visual Studio:error MSB8020
状况如下: error MSB8020: The builds tools for v120 (Platform Toolset = 'v120') cannot be found. To build ...
- java学习之局部变量以及全局变量
全局变量 什么是全局变量? 全局变量就好比一个容器或者一个公用的东西一样,就类似外面公共场所的凳子一样,大家都可以使用这个凳子. 和他相反的局部变量是啥子东东呢? 局部变量就是局部的东西,如果全局变量 ...
- linux下编译 静态库、动态库,动态库依赖静态库
xx.o : xx.h xx.cpp -lstdc++ -o xx.o -c xx.cpp -I ./ libxx.a : xx.o ar -crv libxx.a xx.o libTest.so : ...
- 关于液晶显示器的6bit面板、8bit面板及E-IPS(转)
原文:http://bbs.3dmgame.com/thread-2232447-1-1.html 1.什么是6bit面板.8bit面板 众所周知,液晶显示器 ...
- 如何只利用NMAKE+CL+LINK写WIN32程序
关键是1.包含<Windows.h>及其他的相关头文件2.在LINK指令中最起码要加上KERNEL32.LIB USER32.LIB GDI32.LIB(不需要制定其路径,因为NMAKE, ...
- tableview 与 tableview cell
1.tableview cell: import Foundationimport UIKit class CjwtCell: UITableViewCell { @IBOutlet var lb_c ...
- Spring Mvc中DispatcherServlet和Servlet的区别小结
在web开发过程中开始接触的是servlet,用来处理用户请求.这几年随着spring 框架越来越成熟,几乎成了java web开发界的主流框架.既然这么受欢迎肯定有它的优点,spring框架在原来的 ...
- 如意云路由刷PandoraBox
目录 1 准备固件 2 使用uboot刷机 2.1 修改IP 2.2 开始刷写 3 ssh登录 4 刷回如意云 准备固件 http://downloads.openwrt.org.cn/Pandora ...
- DP - 字符混编
字符混编 Problem's Link ---------------------------------------------------------------------------- Mea ...