Android Listview异步动态加载网络图片
1.定义类MapListImageAndText管理ListViewItem中控件的内容
package com.google.zxing.client.android.AsyncLoadImage; public class MapListImageAndText {
private String imageUrl;
private String shopname;
private String activitynifo;
private String address;
private String telephone;
private String distance; public MapListImageAndText(String imageUrl, String shopname, String activitynifo, String address, String telephone,String distance) {
this.imageUrl = imageUrl;
this.shopname = shopname;
this.activitynifo = activitynifo;
this.address = address;
this.telephone = telephone;
this.distance=distance;
} public String getImageUrl() {
return imageUrl;
} public String getShopname() {
return shopname;
} public String getActivitynifo() {
return activitynifo;
} public String getAddress() {
return address;
} public String getTelephone() {
return telephone;
} public String getDistance() {
return distance;
} }
2. 定义类MapListViewCache实例化ListViewItem中的控件
package com.google.zxing.client.android.AsyncLoadImage; import com.google.zxing.client.android.R; import android.view.View;
import android.widget.ImageView;
import android.widget.TextView; public class MapListViewCache { private View baseView;
private TextView shopname;
private TextView activitynifo;
private TextView address;
private TextView telephone;
private TextView distance; private ImageView imageView; public MapListViewCache(View baseView) {
this.baseView = baseView;
} public TextView getShopname() {
if (shopname == null) {
shopname = (TextView) baseView.findViewById(R.id.maplistviewitemshopname);
}
return shopname;
} public TextView getActivitynifo() {
if (activitynifo == null) {
activitynifo = (TextView) baseView.findViewById(R.id.maplistviewitemActi);
}
return activitynifo;
} public TextView getAddress() {
if (address == null) {
address = (TextView) baseView.findViewById(R.id.maplistviewitemaddr);
}
return address;
} public TextView getTelephone() {
if (telephone == null) {
telephone = (TextView) baseView.findViewById(R.id.maplistviewitemtelphone);
}
return telephone;
} public ImageView getImageView() {
if (imageView == null) {
imageView = (ImageView) baseView.findViewById(R.id.maplistviewitemImage);
}
return imageView;
} public TextView getDistance() {
if (distance == null) {
distance = (TextView) baseView.findViewById(R.id.maplistviewitemdistance);
}
return distance;
} }
3. 定义类AsyncImageLoader,开启线程下载指定图片
package com.google.zxing.client.android.AsyncLoadImage; import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap; import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message; public class AsyncImageLoader { private HashMap<String, SoftReference<Drawable>> imageCache; public AsyncImageLoader() {
imageCache = new HashMap<String, SoftReference<Drawable>>();
} public Drawable loadDrawable(final String imageUrl, final ImageCallback imageCallback) {
if (imageCache.containsKey(imageUrl)) {
SoftReference<Drawable> softReference = imageCache.get(imageUrl);
Drawable drawable = softReference.get();
if (drawable != null) {
return drawable;
}
}
final Handler handler = new Handler() {
public void handleMessage(Message message) {
imageCallback.imageLoaded((Drawable) message.obj, imageUrl);
}
};
new Thread() {
@Override
public void run() {
Drawable drawable = loadImageFromUrl(imageUrl);
imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));
Message message = handler.obtainMessage(, drawable);
handler.sendMessage(message);
}
}.start();
return null;
} public static Drawable loadImageFromUrl(String url) {
URL m;
InputStream i = null;
try {
m = new URL(url);
i = (InputStream) m.getContent();
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Drawable d = Drawable.createFromStream(i, "src");
return d;
} public interface ImageCallback {
public void imageLoaded(Drawable imageDrawable, String imageUrl);
} }
4. 定义类MapListImageAndTextListAdapter继承ArrayAdapter
package com.google.zxing.client.android.AsyncLoadImage; import java.util.List; import com.google.zxing.client.android.R; import com.google.zxing.client.android.AsyncLoadImage.AsyncImageLoader.ImageCallback; import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView; public class MapListImageAndTextListAdapter extends ArrayAdapter<MapListImageAndText> { private ListView listView;
private AsyncImageLoader asyncImageLoader; public MapListImageAndTextListAdapter(Activity activity, List<MapListImageAndText> imageAndTexts, ListView listView) {
super(activity, , imageAndTexts);
this.listView = listView;
asyncImageLoader = new AsyncImageLoader();
} public View getView(int position, View convertView, ViewGroup parent) {
Activity activity = (Activity) getContext(); // Inflate the views from XML
View rowView = convertView;
MapListViewCache viewCache;
if (rowView == null) {
LayoutInflater inflater = activity.getLayoutInflater();
rowView = inflater.inflate(R.layout.maplistviewitem, null);
viewCache = new MapListViewCache(rowView);
rowView.setTag(viewCache);
} else {
viewCache = (MapListViewCache) rowView.getTag();
}
MapListImageAndText imageAndText = getItem(position); // Load the image and set it on the ImageView
String imageUrl = imageAndText.getImageUrl();
ImageView imageView = viewCache.getImageView();
imageView.setTag(imageUrl);
Drawable cachedImage = asyncImageLoader.loadDrawable(imageUrl, new ImageCallback() { public void imageLoaded(Drawable imageDrawable, String imageUrl) {
ImageView imageViewByTag = (ImageView) listView.findViewWithTag(imageUrl);
if (imageViewByTag != null) {
imageViewByTag.setImageDrawable(imageDrawable);
}
}
});
if (cachedImage == null) {
imageView.setImageResource(R.drawable.refresh);
}else{
imageView.setImageDrawable(cachedImage);
}
// Set the text on the TextView
TextView shopname = viewCache.getShopname();
shopname.setText(imageAndText.getShopname()); TextView activitynifo = viewCache.getActivitynifo();
activitynifo.setText(imageAndText.getActivitynifo()); TextView address = viewCache.getAddress();
address.setText(imageAndText.getAddress()); TextView telephone = viewCache.getTelephone();
telephone.setText(imageAndText.getTelephone()); TextView distance = viewCache.getDistance();
distance.setText(imageAndText.getDistance()); return rowView;
} }
5.主程序中Listview与MapListImageAndTextListAdapter的捆绑
//tuangoupoints为对后台传回来的数据解析后得到的字符串
String[] mtuangoupoints =tuangoupoints.split("@"); List<MapListImageAndText> dataArray=new ArrayList<MapListImageAndText>(); for(int i=; i<mtuangoupoints.length;i++){
String[] tonepoint=mtuangoupoints[i].split("#"); String shopname=String.valueOf(i+)+tonepoint[];
String activityinfo=tonepoint[];
String address=tonepoint[];
String telephone=tonepoint[];
String imageurl=tonepoint[];
String distance=tonepoint[]; MapListImageAndText test=new MapListImageAndText(imageurl,shopname,activityinfo,address,telephone,distance);
dataArray.add(test);
} MapListImageAndTextListAdapter adapter=new MapListImageAndTextListAdapter(this, dataArray, mlistView);
mlistView.setAdapter(adapter);
Android Listview异步动态加载网络图片的更多相关文章
- Android高效异步图片加载框架
概述 Android高效异步图片加载框架:一个高效的异步加载显示的图片加载框架,同时具备图片压缩,缓存机制等特性. 详细 代码下载:http://www.demodashi.com/demo/1214 ...
- Android中的动态加载机制
在目前的软硬件环境下,Native App与Web App在用户体验上有着明显的优势,但在实际项目中有些会因为业务的频繁变更而频繁的升级客户端,造成较差的用户体验,而这也恰恰是Web App的优势.本 ...
- Android 实现布局动态加载
Android 动态加载布局 通过使用LayoutInflater 每次点击按钮时候去读取布局文件,然后找到布局文件里面的各个VIEW 操作完VIEW 后加载进我们setContentView 方面里 ...
- android ListView上拉加载更多 下拉刷新功能实现(采用pull-to-refresh)
Android实现上拉加载更多功能以及下拉刷新功能, 采用了目前比较火的PullToRefresh,他是目前实现比较好的下拉刷新的类库. 目前他支持的控件有:ListView, ExpandableL ...
- [Android Pro] so 动态加载—解决sdk过大问题
原文地址: https://blog.csdn.net/Rong_L/article/details/75212472 前言 相信Android 开发中大家或多或少都会集成一些第三方sdk, 而其中难 ...
- Android学习——Fragment动态加载
动态加载原理 利用FragmentManager来添加一套Fragment事务,最后通过commit提交该事务来执行对Fragment的相关操作. FragmentManager fragmentma ...
- 转: listview异步图片加载之优化篇(android)
Listview异步加载之优化篇 关于listview的异步加载,网上其实很多示例了,总体思想差不多,不过很多版本或是有bug,或是有性能问题有待优化.有鉴于此,本人在网上找了个相对理想的版本并在此基 ...
- Android ListView避免多线程加载一个同一资源
当我们的ListView中的Item包含图片,而且这些图片是同一资源,我们用多线程去加载图片,这时候可能就发生了这种情况. 比如线程是人,第一个人去做加载图片到缓存的工作,还没做好时第二个人要这同一张 ...
- PhotoSwipe异步动态加载图片
在开发搜房家居M站的时候,搜房家居装修效果图相册展示效果需要用到PhotoSwipe插件来显示图片.特点:1. 家居提供的接口,每次只能获取一张图片2. 装修效果图的张数不限.3. 从PhotoSwi ...
随机推荐
- BZOJ1089: [SCOI2003]严格n元树
1089: [SCOI2003]严格n元树 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 762 Solved: 387[Submit][Status ...
- 关于as3实现对任何对象进行深刻复制的思考
无意中看到关于as3深度复制思考的文章,觉得不错,于是转来记录以后用到可以参考. 转载来源:http://xmchcly.iteye.com/blog/1307425,下面是转文: 通过 ByteAr ...
- (一)一个简单的Web服务器
万丈高楼平地起,首先我们必须了解 超文本传输协议(HTTP) 以后才能够比较清晰的明白web服务器是怎么回事. 1. 浅析Http协议 HTTP是一种协议,允许web服务器和浏览器通过互联网进行来发送 ...
- Centos 添加Root用户
今天,我要描述的是如何在Centos Linux 系统中建立一个和Root账户等权限的用户账户.废话不多说,开始列出必要的操作. 1:首先,我们使用以下命令 进行用户的创建 和 用户密码的初始化. # ...
- Vericant维立克 | 氪加
Vericant维立克 | 氪加 Vericant维立克
- JAVA获取oracle中sequences的最后一个值
项目中,用到一个序列作单号,框架用的是ssh,在dao层去拿的时候,运行时报错为dual is not mapped,[select *.nextval nextvalue from dual] 后来 ...
- Html5学习笔记(一)
一:常见标签类型 块级标签 特点:1.独占一行 2,可以随时设置w,h 2.行内标签(内联) 特点: 1.多个行内标签能同时显示在一行 2.w.h取决于内容的尺寸() 3.行内-块级标签 特点 ...
- AJAX上传文件
function up_files() { var fileSelect = document.getElementById('file-select'); var files = fileSelec ...
- Android静态变量使用陷阱
静态变量大家再熟悉不过了,本来没什么好重复的.事情起因是这样的,最近测试那边反应正在做的一个产品总是莫名其妙的显示不出某些数据,甚至闪退崩溃,仔细查了几遍发现没什么问题,最后百般周折发现在那部测试机上 ...
- RPG JS跨平台测试
RPG JS虽然说是跨平台的,但是在具体的测试中效果并不理想. 以官方提供的Demo为例 问题一 手机的屏幕太小,导致画面上的人物都很小,连点击都很不准确.在9寸的平板上才可以看得比较清楚. 问题二 ...