ListView Adapter优化 实例
//方案一 - adapter优化public class listviewAdapterDemo1 extends BaseAdapter {
private String[] mArrData;
private ListView listView;
private AsyncImageLoader asyncImageLoader;
private final LayoutInflater inflater;
public listviewAdapterDemo1(String[] mArrData, Context mContext, ListView listView) {
this.mArrData = mArrData;
this.listView = listView;
asyncImageLoader = new AsyncImageLoader();
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return this.mArrData.length;
}
@Override
public Object getItem(int arg0) {
return mArrData[arg0];
}
@Override
public long getItemId(int position) {
return position;
}
/***
* 1.控件对象内存为一个
* 2.view对象只会创建可视对象次数(有的是8次)根据屏幕可视listview的数量而定,在加载超过可视postion数据时不会重新创建
* 但是需要每次重新加载数据
*
* 从节省内存的角度出发,这种更合理,毕竟加载数据不需要占用内存
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewholder;
Log.d("dd", "Position:" + position+"------");
if (convertView == null) {
Log.d("dd", "Position:" + position);
convertView = inflater.inflate(R.layout.list_item_icon_text, null);
viewholder = new ViewHolder();
viewholder.icon = (ImageView) convertView.findViewById(R.id.icon);
viewholder.text = (TextView) convertView.findViewById(R.id.text);
convertView.setTag(viewholder);
} else {
viewholder = (ViewHolder) convertView.getTag();
}
viewholder.text.setText(mArrData[position]);
// Load the image and set it on the ImageView
String imageUrl = "";
viewholder.icon.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)//说明是从缓存池中得到
viewholder.icon.setImageDrawable(cachedImage);
return convertView;
}
static class ViewHolder {
ImageView icon;
TextView text;
}
}
在listview中的getview方法中也可以这样用这个方法:
代码如下:
public Drawable getDrawable(AsyncImageLoader asyncImageLoader, String imageUrl, final ImageView imageView) {
Drawable drawable = asyncImageLoader.loadDrawable(imageUrl,new ImageCallback() {
@Override public void imageLoaded(Drawable imageDrawable,String imageUrl) {
if (imageDrawable != null)
imageView.setImageDrawable(imageDrawable);
else
imageView.setImageResource(R.drawable.u6_normal);
}
});
return drawable;
}
//方案二---adapter的优化public class listviewAdapterDemo2 extends BaseAdapter {
private String[] mArrData;
private ListView listView;
private AsyncImageLoader asyncImageLoader;
private final LayoutInflater inflater;
private Map<Integer, View> viewMap = new HashMap<Integer, View>();
public listviewAdapterDemo2(String[] mArrData, Context mContext, ListView listView) {
this.mArrData = mArrData;
this.listView = listView;
asyncImageLoader = new AsyncImageLoader();
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return this.mArrData.length;
}
@Override
public Object getItem(int arg0) {
return mArrData[arg0];
}
@Override
public long getItemId(int position) {
return position;
}
/**
* 1.控件对象内存为一个
* 2.每一个postion对应一个view,且不管前进后退,每个postion数据只加载一次 * 但是从节省内容的角度出发,方案一更节省内存
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewholder;
Log.d("dd", "Position:" + position+"------");
convertView = this.viewMap.get(position);
if (convertView == null) {
Log.d("dd", "Position:" + position);
convertView = inflater.inflate(R.layout.list_item_icon_text, null);
viewholder = new ViewHolder();
viewholder.icon = (ImageView) convertView.findViewById(R.id.icon);
viewholder.text = (TextView) convertView.findViewById(R.id.text);
viewholder.text.setText(mArrData[position]);
// Load the image and set it on the ImageView
String imageUrl = "";
viewholder.icon.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)//说明是从缓存池中得到
viewholder.icon.setImageDrawable(cachedImage);
convertView.setTag(viewholder);
viewMap.put(position, convertView);
} else {
viewholder = (ViewHolder) convertView.getTag();
}
return convertView;
}
static class ViewHolder {
ImageView icon;
TextView text;
}
}
/**图片下载-adpater优化*/
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() {
@Override
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) {
Drawable drawable = null;
//根据url从服务器下载图片
return drawable;
}
public interface ImageCallback {
public void imageLoaded(Drawable imageDrawable, String imageUrl);
}
}
//activity---adapterpublic class ListviewDemoActivity extends Activity {
private listviewAdapterDemo1 mAdapter;
private ListView listview;
private String[] mArrData;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mArrData = new String[1000];
for (int i = 0; i < 1000; i++) {
mArrData[i] = "Google IO Adapter" + i;
}
listview = (ListView)findViewById(R.id.lv);
mAdapter = new listviewAdapterDemo1(mArrData,this,listview);
listview.setAdapter(mAdapter);
}
}
<ListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />