MainActivity.java如下:
package cn.ideallistview; import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ListView; //ListView中的Item显示一张网络图片 //流程: //1 试图从内存的缓存中获取 // 1.1从一级缓存(强引用)中查找图片 // 1.2从二级缓存(若引用)中查找图片 // 若1失败转入2 //2 试图从SDCard的File中获取 // 若2失败转入3 //3 从网络下载图片 // 3.1将下载后的图片保存至SDCard的File中 // 3.2将从SDCard的File中获取的图片返回给ListView显示 // 3.3将从SDCard的File中获取的图片保存至内存的缓存 // 3.3.1若一级缓存剩余空间足够保存该图片则保存至一级缓存 // 若失败转入3.3.2 // 3.3.2将一级缓存中最陈旧的元素放入二级缓存. // 再次核实一级缓存存剩余空间,直至有足够空间存放图片 public class MainActivity extends Activity { ListView listView; ListViewAdapter adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initView(); } public void initView() { Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new ButtonOnClickListener()); listView = (ListView) findViewById(R.id.listView); ArrayList<ListViewItem> arrayList = new ArrayList<ListViewItem>(); ListViewItem item1 = new ListViewItem ("xxxx","http://images.csdn.net/20121018/zazhi-68-78-1018.jpg"); arrayList.add(item1); ListViewItem item2 = new ListViewItem ("xxxx","http://info-database.csdn.net/Upload/2012-10-08/zazhi-210-90-1008.jpg"); arrayList.add(item2); ListViewItem item3 = new ListViewItem ("xxxx","http://images.csdn.net/20121119/20111211223655841.jpg"); arrayList.add(item3); ListViewItem item4 = new ListViewItem ("xxxx","http://images.csdn.net/20121119/20120619174604972.jpg"); arrayList.add(item4); ListViewItem item5 = new ListViewItem ("xxxx","http://csdnimg.cn/www/images/pic_foot_report110.png"); arrayList.add(item5); ListViewItem item6 = new ListViewItem ("xxxx","http://csdnimg.cn/www/images/pic_foot_report.png"); arrayList.add(item6); ListViewItem item7 = new ListViewItem ("xxxx","http://csdnimg.cn/www/images/pic_foot_BNIA.png"); arrayList.add(item7); ListViewItem item8 = new ListViewItem ("xxxx","http://csdnimg.cn/www/images/pic_foot_gongshang.png"); arrayList.add(item8); ListViewItem item9 = new ListViewItem ("xxxx","http://images.csdn.net/20120803/logo-qixing02.jpg"); arrayList.add(item9); ListViewItem item10 = new ListViewItem ("xxxx","http://images.csdn.net/20120726/quanjing-logo-shouye.jpg"); arrayList.add(item10); ListViewItem item11 = new ListViewItem ("xxxx","http://images.csdn.net/20120726/nhn-logo-shouye.jpg"); arrayList.add(item11); ListViewItem item12 = new ListViewItem ("xxxx","http://images.csdn.net/20120510/shanghai-jiaoda-logo.jpg"); arrayList.add(item12); ListViewItem item13 = new ListViewItem ("xxxx","http://images.csdn.net/20120312/bigman2.gif"); arrayList.add(item13); ListViewItem item14 = new ListViewItem ("xxxx","http://images.csdn.net/20120216/csdn2.gif"); arrayList.add(item14); ListViewItem item15 = new ListViewItem ("xxxx","http://images.csdn.net/20121109/win8_100x74.jpg"); arrayList.add(item15); ListViewItem item16 = new ListViewItem ("xxxx","http://images.csdn.net/20120816/cf-20120816.jpg"); arrayList.add(item16); ListViewItem item17 = new ListViewItem ("xxxx","http://images.csdn.net/20120704/bi05.jpg"); arrayList.add(item17); ListViewItem item18 = new ListViewItem ("xxxx","http://images.csdn.net/20120816/amd-20120816.jpg"); arrayList.add(item18); ListViewItem item19 = new ListViewItem ("xxxx","http://y2.ifengimg.com/2012/12/08/09383369.jpg"); arrayList.add(item19); ListViewItem item20 = new ListViewItem ("xxxx","http://y1.ifengimg.com/2012/12/08/09320040.jpg"); arrayList.add(item20); ListViewItem item21 = new ListViewItem ("xxxx","http://y3.ifengimg.com/4ccd6020154f55cb/2012/1207/2.jpg"); arrayList.add(item21); ListViewItem item22 = new ListViewItem ("xxxx","http://y2.ifengimg.com/2012/12/08/09425571.jpg"); arrayList.add(item22); ListViewItem item23 = new ListViewItem ("xxxx","http://y2.ifengimg.com/b19fadab6fcc0645/2012/1011/123.jpg"); arrayList.add(item23); ListViewItem item24 = new ListViewItem ("xxxx","http://y2.ifengimg.com/2012/11/15/07414772.jpg"); arrayList.add(item24); ListViewItem item25 = new ListViewItem ("xxxx","http://y2.ifengimg.com/2012/11/15/07532374.jpg"); arrayList.add(item25); ListViewItem item26 = new ListViewItem ("xxxx","http://y0.ifengimg.com/2012/11/15/07583017.jpg"); arrayList.add(item26); ListViewItem item27 = new ListViewItem ("xxxx","http://y0.ifengimg.com/2012/02/20/14175732.jpg"); arrayList.add(item27); ListViewItem item28 = new ListViewItem ("xxxx","http://y2.ifengimg.com/2012/10/29/21175274.jpg"); arrayList.add(item28); ListViewItem item29 = new ListViewItem ("xxxx","http://res.news.ifeng.com/2727e09814adca26/2011/1221/rdn_4ef0fb0333564.jpg"); arrayList.add(item29); ListViewItem item30 = new ListViewItem ("xxxx","http://y1.ifengimg.com/2012/12/04/10060551.jpg"); arrayList.add(item30); ListViewItem item31 = new ListViewItem ("xxxx","http://res.news.ifeng.com/b77953a0c6aae8fb/2011/1114/rdn_4ec068537d8f6.jpg"); arrayList.add(item31); ListViewItem item32 = new ListViewItem ("xxxx","http://res.news.ifeng.com/b77953a0c6aae8fb/2011/1114/rdn_4ec068554fa3f.jpg"); arrayList.add(item32); ListViewItem item33 = new ListViewItem ("xxxx","http://res.news.ifeng.com/b77953a0c6aae8fb/2011/1114/rdn_4ec06853bd85b.jpg"); arrayList.add(item33); ListViewItem item34 = new ListViewItem ("xxxx","http://res.news.ifeng.com/b77953a0c6aae8fb/2011/1114/rdn_4ec06854ace88.jpg"); arrayList.add(item34); ListViewItem item35 = new ListViewItem ("xxxx","http://res.news.ifeng.com/b77953a0c6aae8fb/2011/1114/rdn_4ec06855bad01.jpg"); arrayList.add(item35); ListViewItem item36 = new ListViewItem ("xxxx","http://res.news.ifeng.com/b77953a0c6aae8fb/2011/1114/rdn_4ec0685649c4e.jpg"); arrayList.add(item36); ListViewItem item37 = new ListViewItem ("xxxx","http://res.news.ifeng.com/b77953a0c6aae8fb/2011/1114/rdn_4ec068562ac3d.jpg"); arrayList.add(item37); ListViewItem item38 = new ListViewItem ("xxxx","http://res.news.ifeng.com/b77953a0c6aae8fb/2011/1114/rdn_4ec06854dc836.jpg"); arrayList.add(item38); ListViewItem item39 = new ListViewItem ("xxxx","http://res.news.ifeng.com/b77953a0c6aae8fb/2011/1114/rdn_4ec06855ef0ea.jpg"); arrayList.add(item39); // ListViewItem item40 = new ListViewItem // ("xxxx","http://res.news.ifeng.com/b77953a0c6aae8fb/2011/1114/rdn_4ec068568beda.jpg"); // arrayList.add(item40); ListViewItem item40 = new ListViewItem ("xxxx","http://s12.sinaimg.cn/middle/4b181bd0gbf6cb07c2a1b&690"); arrayList.add(item40); adapter = new ListViewAdapter(arrayList, MainActivity.this); listView.setAdapter(adapter); } @Override public void onDestroy() { listView.setAdapter(null); super.onDestroy(); } private class ButtonOnClickListener implements OnClickListener{ @Override public void onClick(View v) { adapter.imageLoader.clearCache(); //adapter.notifyDataSetChanged(); } } }
ListViewAdapter.java如下:
package cn.ideallistview; import java.util.ArrayList; import android.content.Context; 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 cn.loadImages.ImageLoader; public class ListViewAdapter extends BaseAdapter { private static LayoutInflater inflater=null; public ImageLoader imageLoader; private ArrayList<ListViewItem> mArrayList; private Context mContext; public ListViewAdapter(ArrayList<ListViewItem> mArrayList,Context mContext) { super(); this.mArrayList = mArrayList; this.mContext = mContext; inflater=(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); imageLoader=new ImageLoader(mContext); } 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=inflater.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 { String imageUrl=listViewItem.getImageURL(); //加载网络上的图片 imageLoader.displayImage(imageUrl, holder.imageView); } catch (Exception e) { e.printStackTrace(); } } } return convertView; } private class ViewHolder{ ImageView imageView; TextView textView; } }
ListViewItem.java如下:
package cn.ideallistview; public class ListViewItem { String content; String imageURL; public ListViewItem(String content, String imageURL) { super(); this.content = content; this.imageURL = imageURL; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getImageURL() { return imageURL; } public void setImageURL(String imageURL) { this.imageURL = imageURL; } }
main.xml如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ListView android:id="@+id/listView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"/> <Button android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Clear FileCache"/> </LinearLayout>
listviewitem.xml如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/imageView" android:layout_width="50dip" android:layout_height="50dip" android:src="@drawable/stub" android:scaleType="centerCrop"/> <TextView android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="left|center_vertical" android:textSize="20dip" android:layout_marginLeft="10dip"/> </LinearLayout>