Android實現(xiàn)Listview異步加載網(wǎng)絡(luò)圖片并動態(tài)更新的方法
本文實例講述了Android實現(xiàn)Listview異步加載網(wǎng)絡(luò)圖片并動態(tài)更新的方法。分享給大家供大家參考,具體如下:
應(yīng)用實例:解析后臺返回的數(shù)據(jù),把每條都顯示在ListView中,包括活動圖片、店名、活動詳情、地址、電話和距離等。
在布局文件中ListView的定義:
<ListView android:id="@id/maplistview" android:background="@drawable/bg" android:layout_width="fill_parent" android:layout_height="fill_parent" android:cacheColorHint="#00000000" android:divider="@drawable/separator" android:dividerHeight="2.0px" android:layout_below="@id/mapseparator" />
在布局文件ListViewItem,中定義活動圖片、店名、活動詳情、地址、電話和距離的布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:paddingBottom="2dip" android:paddingLeft="2dip" android:paddingRight="2dip"> <ImageView android:paddingTop="2dip" android:layout_alignParentLeft="true" android:layout_width="80px" android:layout_height="80px" android:id="@+id/maplistviewitemImage"/> <TextView android:layout_height="wrap_content" android:textSize="17dip" android:layout_width="fill_parent" android:id="@+id/maplistviewitemshopname" android:layout_toRightOf="@id/maplistviewitemImage" android:textColor="#000000"/> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_alignParentLeft="true" android:layout_below="@+id/maplistviewitemImage" android:id="@+id/maplistviewitemActi" android:textColor="#6C6C6C"/> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_alignParentLeft="true" android:layout_below="@+id/maplistviewitemActi" android:id="@+id/maplistviewitemaddr" android:textColor="#6C6C6C" android:singleLine="true"/> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_alignParentLeft="true" android:layout_below="@+id/maplistviewitemaddr" android:id="@+id/maplistviewitemtelphone" android:textColor="#6C6C6C" android:singleLine="true"/> </RelativeLayout>
(1)定義類MapListImageAndText管理ListViewItem中控件的內(nèi)容
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(0, 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,用于創(chuàng)建AsyncImageLoader實例,并指定控件的內(nèi)容
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, 0, 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為對后臺傳回來的數(shù)據(jù)解析后得到的字符串
String[] mtuangoupoints =tuangoupoints.split("@");
List<MapListImageAndText> dataArray=new ArrayList<MapListImageAndText>();
for(int i=0; i<mtuangoupoints.length;i++){
String[] tonepoint=mtuangoupoints[i].split("#");
String shopname=String.valueOf(i+1)+tonepoint[2];
String activityinfo=tonepoint[1];
String address=tonepoint[6];
String telephone=tonepoint[7];
String imageurl=tonepoint[8];
String distance=tonepoint[5];
MapListImageAndText test=new MapListImageAndText(imageurl,shopname,activityinfo,address,telephone,distance);
dataArray.add(test);
}
MapListImageAndTextListAdapter adapter=new MapListImageAndTextListAdapter(this, dataArray, mlistView);
mlistView.setAdapter(adapter);
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android操作SQLite數(shù)據(jù)庫技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android開發(fā)入門與進階教程》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
- android CursorLoader用法介紹
- android異步加載圖片并緩存到本地實現(xiàn)方法
- Android中ListView異步加載圖片錯位、重復(fù)、閃爍問題分析及解決方案
- Android 異步加載圖片分析總結(jié)
- Android中使用二級緩存、異步加載批量加載圖片完整案例
- Android加載對話框同時異步執(zhí)行實現(xiàn)方法
- Android 異步加載圖片的實例代碼
- Android App中實現(xiàn)圖片異步加載的實例分享
- Android程序開發(fā)ListView+Json+異步網(wǎng)絡(luò)圖片加載+滾動翻頁的例子(圖片能緩存,圖片不錯亂)
- 使用CursorLoader異步加載數(shù)據(jù)
相關(guān)文章
Android liveData與viewBinding使用教程
LiveData是一種可觀察的數(shù)據(jù)存儲器類,LiveData使用觀察者模式,每當(dāng)數(shù)據(jù)發(fā)生變化時,LiveData會通知 Observer對象,我們可以在這些 Observer 對象中更新UI,ViewModel對象為特定的界面組件提供數(shù)據(jù),并包含數(shù)據(jù)處理業(yè)務(wù)邏輯,會配合LiveData一起使用2022-11-11
Android如何實現(xiàn)社交應(yīng)用中的評論與回復(fù)功能詳解
目前,各種App的社區(qū)或者用戶曬照片、發(fā)說說的地方,都提供了評論功能,為了更好地學(xué)習(xí),自己把這個功能實現(xiàn)了一下,下面這篇文章主要給大家介紹了關(guān)于Android如何實現(xiàn)社交應(yīng)用中的評論與回復(fù)功能的相關(guān)資料,需要的朋友可以參考下2018-07-07
SwipeLayout框架實現(xiàn)側(cè)拉刪除編輯功能
這篇文章主要為大家詳細(xì)介紹了SwipeLayout框架實現(xiàn)側(cè)拉刪除編輯功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08
Android設(shè)置PreferenceCategory背景顏色的方法
這篇文章主要介紹了Android設(shè)置PreferenceCategory背景顏色的方法,涉及Android設(shè)置背景色的技巧,需要的朋友可以參考下2015-05-05
Android6.0 動態(tài)權(quán)限機制深入講解
這篇文章主要給大家介紹了關(guān)于Android6.0 動態(tài)權(quán)限機制的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Android自定義控件ImageView實現(xiàn)點擊之后出現(xiàn)陰影效果
這篇文章主要為大家詳細(xì)介紹了Android自定義控件ImageView實現(xiàn)點擊之后有陰影效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
Android ViewPager與radiogroup實現(xiàn)關(guān)聯(lián)示例
本篇文章主要介紹了Android ViewPager與radiogroup實現(xiàn)關(guān)聯(lián)示例,具有一定的參考價值,有興趣的可以了解一下。2017-03-03
ProgressBar、ProgessDialog-用法(詳解)
下面小編就為大家?guī)硪黄狿rogressBar、ProgessDialog-用法(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06

