MainActivity.java如下:
package com.cn.perfectlistview; import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; //解决的问题: //1 ListView异步加载网络图片 //2 ListView滑动时,图片错位 public class MainActivity extends Activity { private ListView listView; MyListViewAdapter adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initView(); } public void initView(){ 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); adapter = new MyListViewAdapter(MainActivity.this, arrayList); listView.setAdapter(adapter); } }
MyListViewAdapter.java如下:
package com.cn.perfectlistview; 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 com.cn.loadImages.ImageDownloader; public class MyListViewAdapter extends BaseAdapter{ private ArrayList<ListViewItem> mArrayList; private Context mContext; private ImageDownloader imageDownloader; public MyListViewAdapter(Context mContext,ArrayList<ListViewItem> mArrayList) { super(); this.mArrayList = mArrayList; this.mContext = mContext; String folderNameOnSDCard="images"; imageDownloader=new ImageDownloader(mContext, folderNameOnSDCard); } 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 { String imageUrl=listViewItem.getImageURL(); //加载网络上的图片 imageDownloader.download(imageUrl, holder.imageView); } catch (Exception e) { e.printStackTrace(); } } } return convertView; } private class ViewHolder{ ImageView imageView; TextView textView; } }
ListViewItem.java如下:
package com.cn.perfectlistview; 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; } }