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 ...
随机推荐
- Visual Studio 启动加速
Who is locking my SQL database?|Deploy a database project with TFS Build Visual Studio 2012 running ...
- Eclipse中安装TestNG插件
在Eclipse中安装TestNG也像安装其他插件一样非常方便,如下: 选择菜单:Help->Install New Software,然后在弹出窗口中的“Work with”中输入地址: ht ...
- CodeForces 19D Points
Pete and Bob invented a new interesting game. Bob takes a sheet of paper and locates a Cartesian coo ...
- html p标签换行问题
/*p标签自动换行*/ p{ word-wrap:break-word; word-break:normal; } /*p强制不换行*/ p{ white-space:nowrap; } /*块级元素 ...
- 关于bootstrap--表单(水平表单)
在Bootstrap框架中要实现水平表单效果,必须满足以下两个条件:1.在<form>元素是使用类名“form-horizontal”.2.配合Bootstrap框架的网格系统.(网格布局 ...
- Java中BigDecimal的8种舍入模式是怎样的
Java中BigDecimal的8种舍入模式是怎样的?下面长沙欧柏泰克软件学院和大家一起来学习下吧: java.math.BigDecimal 不可变的.任意精度的有符号十进制数.BigDecima ...
- js笔记01
js编写页面特效动态脚本类型的语言变量:存储数据(日常生活中的东西,比如电视,手机,电脑,出生年份...)语法: var obj=value; obj不能为数字开头,且区分大小写 value对应数据类 ...
- 关于DevExpress的gridControl的简单使用
数据绑定 首先生成table,然后更改列名,最后添加一个选择列,类型为"System.Boolean",这样在绑定上gridcontrol的时候会出现一列选择框 table.Col ...
- webService返回自定义类型的数据处理
1.自定义一个Student 数据类型: package com.chnic.webservice; import java.io.Serializable; public class Student ...
- FragmentPagerAdapter与FragmentStatePagerAdapter差异
平常使用的FragmentPagerAdapter和FragmentStatePagerAdapter来自android.support.v4.app包用来构建ViewPager. FragmentP ...