MainActivity如下:
package lee.listviewimage;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import lee.listviewimage.R;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.ListView;import android.widget.SimpleAdapter;//解決的問題://1 ListView非同步載入網狀圖片//2 ListView滑動時,圖片錯位public class MainActivity extends Activity { ListView listView; List<Map<String, Object>> arrayList; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listView = (ListView) findViewById(R.id.listView); arrayList = new ArrayList<Map<String, Object>>(); Map<String, Object> tempHashMap = new HashMap<String, Object>(); tempHashMap.put("title", "title111"); tempHashMap.put("info", "111111"); tempHashMap.put("img", "http://tb.himg.baidu.com/sys/portrait/item/d71e5a30323837797979d300"); arrayList.add(tempHashMap); tempHashMap = new HashMap<String, Object>(); tempHashMap.put("title", "title222"); tempHashMap.put("info", "222222"); tempHashMap.put("img", "http://img.baidu.com/img/post-jg.gif"); arrayList.add(tempHashMap); tempHashMap = new HashMap<String, Object>(); tempHashMap.put("title", "title222"); tempHashMap.put("info", "222222"); tempHashMap.put("img", "http://lh5.ggpht.com/_mrb7w4gF8Ds/TCpetKSqM1I/AAAAAAAAD2c/Qef6Gsqf12Y/s144-c/_DSC4374%20copy.jpg"); arrayList.add(tempHashMap); tempHashMap = new HashMap<String, Object>(); tempHashMap.put("title", "title222"); tempHashMap.put("info", "222222"); tempHashMap.put("img", "http://lh5.ggpht.com/_Z6tbBnE-swM/TB0CryLkiLI/AAAAAAAAVSo/n6B78hsDUz4/s144-c/_DSC3454.jpg"); arrayList.add(tempHashMap); tempHashMap = new HashMap<String, Object>(); tempHashMap.put("title", "title222"); tempHashMap.put("info", "222222"); tempHashMap.put("img", "http://lh3.ggpht.com/_GEnSvSHk4iE/TDSfmyCfn0I/AAAAAAAAF8Y/cqmhEoxbwys/s144-c/_MG_3675.jpg"); arrayList.add(tempHashMap); tempHashMap = new HashMap<String, Object>(); tempHashMap.put("title", "title222"); tempHashMap.put("info", "222222"); tempHashMap.put("img", "http://lh6.ggpht.com/_Nsxc889y6hY/TBp7jfx-cgI/AAAAAAAAHAg/Rr7jX44r2Gc/s144-c/IMGP9775a.jpg"); arrayList.add(tempHashMap); tempHashMap = new HashMap<String, Object>(); tempHashMap.put("title", "title222"); tempHashMap.put("info", "222222"); tempHashMap.put("img", "http://lh3.ggpht.com/_lLj6go_T1CQ/TCD8PW09KBI/AAAAAAAAQdc/AqmOJ7eg5ig/s144-c/Juvenile%20Gannet%20despute.jpg"); arrayList.add(tempHashMap); tempHashMap = new HashMap<String, Object>(); tempHashMap.put("title", "title222"); tempHashMap.put("info", "222222"); tempHashMap.put("img", "http://lh6.ggpht.com/_ZN5zQnkI67I/TCFFZaJHDnI/AAAAAAAABVk/YoUbDQHJRdo/s144-c/P9250508.JPG"); arrayList.add(tempHashMap); tempHashMap = new HashMap<String, Object>(); tempHashMap.put("title", "title222"); tempHashMap.put("info", "222222"); tempHashMap.put("img", "http://lh6.ggpht.com/_lnDTHoDrJ_Y/TBvKsJ9qHtI/AAAAAAAAG6g/Zll2zGvrm9c/s144-c/000007.JPG"); arrayList.add(tempHashMap); tempHashMap = new HashMap<String, Object>(); tempHashMap.put("title", "title222"); tempHashMap.put("info", "222222"); tempHashMap.put("img", "http://info-database.csdn.net/Upload/2012-10-08/zazhi-210-90-1008.jpg"); arrayList.add(tempHashMap); tempHashMap = new HashMap<String, Object>(); tempHashMap.put("title", "title222"); tempHashMap.put("info", "222222"); tempHashMap.put("img", "http://images.csdn.net/20121119/20111211223655841.jpg"); arrayList.add(tempHashMap); tempHashMap = new HashMap<String, Object>(); tempHashMap.put("title", "title222"); tempHashMap.put("info", "222222"); tempHashMap.put("img", "http://csdnimg.cn/www/images/pic_foot_report110.png"); arrayList.add(tempHashMap); tempHashMap = new HashMap<String, Object>(); tempHashMap.put("title", "title222"); tempHashMap.put("info", "222222"); tempHashMap.put("img", "http://images.csdn.net/20121119/20120619174604972.jpg"); arrayList.add(tempHashMap); tempHashMap = new HashMap<String, Object>(); tempHashMap.put("title", "title222"); tempHashMap.put("info", "222222"); tempHashMap.put("img", "http://images.csdn.net/20121018/zazhi-68-78-1018.jpg"); arrayList.add(tempHashMap); tempHashMap = new HashMap<String, Object>(); tempHashMap.put("title", "title222"); tempHashMap.put("info", "222222"); tempHashMap.put("img", "http://csdnimg.cn/www/images/pic_foot_report.png"); arrayList.add(tempHashMap); tempHashMap = new HashMap<String, Object>(); tempHashMap.put("title", "title222"); tempHashMap.put("info", "222222"); tempHashMap.put("img", "http://www.iteye.com/upload/logo/blog_wiki/711943/3a7f0290-47e6-3d62-8845-49735bc19a96.jpg?1343020143"); arrayList.add(tempHashMap); tempHashMap = new HashMap<String, Object>(); tempHashMap.put("title", "title222"); tempHashMap.put("info", "222222"); tempHashMap.put("img", "http://www.iteye.com/upload/logo/user/731519/c828deb2-5350-3fc7-83e7-b0eaa047d804-thumb.jpg?1346417059"); arrayList.add(tempHashMap); tempHashMap = new HashMap<String, Object>(); tempHashMap.put("title", "title222"); tempHashMap.put("info", "222222"); tempHashMap.put("img", "http://www.iteye.com/upload/logo/user/766328/b7d5f98d-f682-3d54-b697-6b9bacbcd534-thumb.jpg?1352965075"); arrayList.add(tempHashMap); tempHashMap = new HashMap<String, Object>(); tempHashMap.put("title", "title222"); tempHashMap.put("info", "222222"); tempHashMap.put("img", "http://www.iteye.com/upload/logo/user/40888/c82ef195-e561-3ac1-8714-a905052eae6d-thumb.jpg?1236834197"); arrayList.add(tempHashMap); //利用此binder對象為ListViewItem中的小各個小控制項綁定資料.此例子中未操作 //注意:setViewValue方法的傳回值很重要 //即:bound=binder.setViewValue(everyViewOfThisHolder, data, textRepresentation); //一般的思路: //若此處返回false表示在TestSimpleAdapter裡面繼續對每個item進行資料的綁定. //若此處返回true表示在TestSimpleAdapter裡面需要再對每個item進行資料的綁定 //當然具體情況還是視具體的商務邏輯而定 SimpleAdapter.ViewBinder binder = new SimpleAdapter.ViewBinder() {@Overridepublic boolean setViewValue(View view, Object data,String textRepresentation) {return false;}}; //listView原理http://www.xuanyusong.com/archives/1252 TestSimpleAdapter adapter = new TestSimpleAdapter( this,arrayList, R.layout.listviewitem, new String[] {"title", "info", "img" }, new int[] {R.id.title, R.id.info,R.id.imageView },binder);//為此adapter設定ViewBinder adapter.setViewBinder(binder); listView.setAdapter(adapter);} }
自訂SimpleAdapter如下:
package lee.listviewimage;import java.util.HashMap;import java.util.List;import java.util.Map;import android.content.Context;import android.graphics.drawable.Drawable;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Checkable;import android.widget.ImageView;import android.widget.SimpleAdapter;import android.widget.TextView;public class TestSimpleAdapter extends SimpleAdapter { private AsyncImageLoader imageLoader = new AsyncImageLoader(); //按照ListViewItem在ListView中的位置儲存了此ListViewItem對應的View private Map<Integer, View> allItemsViewHashMap = new HashMap<Integer, View>(); private LayoutInflater mInflater; private ViewBinder mViewBinder; private List<? extends Map<String, ?>> mData; private int mResource; private String[] mFrom; private int[] mTo; //構造方法 public TestSimpleAdapter(Context context, List<? extends Map<String, ?>> data,int resource, String[] from, int[] to,ViewBinder binder) { super(context, data, resource, from, to); mData = data; mResource = resource; mFrom = from; mTo = to; mViewBinder=binder; mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public View getView(int position, View convertView, ViewGroup parent) { return createViewFromResource(position, convertView, parent, mResource); } private View createViewFromResource(int position, View convertView, ViewGroup parent, int resource) { View everyItemView = this.allItemsViewHashMap.get(position); if (everyItemView == null) { everyItemView = mInflater.inflate(resource, null); final int[] to = mTo; final int count = to.length; final View[] allPartsOfThisItem = new View[count]; //將此Item中的所有子View存放在allPartsOfThisItem中 for (int i = 0; i < count; i++) { allPartsOfThisItem[i] = everyItemView.findViewById(to[i]); } //給此itemView設定Tag!!! everyItemView.setTag(allPartsOfThisItem); //調用bindView.注意查看SimpleAdapter源碼中也有這個方法.名字都一樣 //上一句setTag了,那麼按照以往BaseAdapter的做法 //後面就該為此item中的各個小控制項賦值了,即在bindView中進行 bindView(position, everyItemView); //將此Item對應的View儲存到HashMap中 allItemsViewHashMap.put(position, everyItemView); } return everyItemView; } //每個Item都會調用此方法 //ViewBinder相當於設定了綁定規則 //類似於BaseAdapter中getView中後半部分為此Item各控制項賦值的處理 @SuppressWarni