MainActivity如下:
package cc.testlistview;
import java.util.ArrayList;
import com.example.testlistview.R;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.widget.ListView;
/**
* Demo描述:
* 利用第三方工具universal-image-loader为ListView异步加载图片
*
* 参考文档:
* 1 http://blog.csdn.net/hkg1pek/article/details/9057815
* 2 http://blog.csdn.net/wwj_748/article/details/10079311
* Thank you very much
*
*/
public class MainActivity extends Activity {
private ListView mListView;
private ImageLoader mImageLoader;
private Context mContext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initView();
initImageLoader(this);
}
public void initView() {
mContext=this;
mImageLoader=ImageLoader.getInstance();
mListView = (ListView) findViewById(R.id.listView);
ArrayList<ListViewItem> arrayList = new ArrayList<ListViewItem>();
String imagesUrl[] = ImagesUrl.Urls;
ListViewItem listViewItem = null;
for (int i = 0; i < imagesUrl.length; i++) {
listViewItem = new ListViewItem("This is " + i, imagesUrl[i]);
arrayList.add(listViewItem);
}
ListViewAdapter adapter = new ListViewAdapter(arrayList,mContext,mImageLoader);
mListView.setAdapter(adapter);
}
/**
* Configuration of ImageLoader:
* This configuration tuning is custom.
* You can tune every option, you may tune some of them,
* or you can create default configuration by
* ImageLoaderConfiguration.createDefault(this) method.
*
* Note:
* 1 enableLogging() // Not necessary in common
* 2 实际项目中可将其放到Application中
*/
public static void initImageLoader(Context context) {
ImageLoaderConfiguration config = new ImageLoaderConfiguration
.Builder(context)
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.discCacheFileNameGenerator(new Md5FileNameGenerator())
.tasksProcessingOrder(QueueProcessingType.LIFO)
.enableLogging()
.build();
ImageLoader.getInstance().init(config);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mImageLoader!=null) {
mImageLoader.clearMemoryCache();
mImageLoader.clearDiscCache();
}
}
}
ListViewAdapter如下:
package cc.testlistview;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.testlistview.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
public class ListViewAdapter extends BaseAdapter{
private ArrayList<ListViewItem> mArrayList;
private Context mContext;
private ImageLoader mImageLoader;
private DisplayImageOptions mDisplayImageOptions;
private ImageLoadingListenerImpl mImageLoadingListenerImpl;
public ListViewAdapter(ArrayList<ListViewItem> arrayList,Context context, ImageLoader imageLoader) {
super();
this.mArrayList = arrayList;
this.mContext = context;
this.mImageLoader = imageLoader;
int defaultImageId = R.drawable.ic_launcher;
mDisplayImageOptions = new DisplayImageOptions.Builder()
.showStubImage(defaultImageId)
.showImageForEmptyUri(defaultImageId)
.showImageOnFail(defaultImageId)
.cacheInMemory(true)
.cacheOnDisc(true)
.resetViewBeforeLoading()
.build();
mImageLoadingListenerImpl=new ImageLoadingListenerImpl();
}
public int getCount() {
if (mArrayList==null) {
return 0;
} else {
return mArrayList.size();
}
}
public Object getItem(int position) {
if (mArrayList==null) {
return null;
} else {
return mArrayList.get(position);
}
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder=null;
if (convertView==null) {
holder=new ViewHolder();
convertView=LayoutInflater.from(this.mContext).inflate(R.layout.listviewitem, null, false);
holder.textView=(TextView) convertView.findViewById(R.id.textView);
holder.imageView=(ImageView) convertView.findViewById(R.id.imageview);
convertView.setTag(holder);
} else {
holder=(ViewHolder) convertView.getTag();
}
if (this.mArrayList!=null) {
ListViewItem listViewItem=this.mArrayList.get(position);
if (holder.textView!=null) {
holder.textView.setText(listViewItem.getContent());
}
if (holder.imageView!=null) {
try {
//加载图片
mImageLoader.displayImage(
listViewItem.imageURL,
holder.imageView,
mDisplayImageOptions,
mImageLoadingListenerImpl);
} catch (Exception e) {