class MyAdapter extends BaseAdapter{ Listlist = new ArrayList (); Context mContext; LayoutInflater mInflater; public MyAdapter(Context context) { this.mContext = context; this.mInflater = LayoutInflater.from(context); JSONArray jsonObject = new JSONArray();//模拟图片混排 jsonObject.put("http://e.hiphotos.baidu.com/album/w%3D2048/sign=c54faa473ac79f3d8fe1e3308e99cc11/7a899e510fb30f244ed503bbc995d143ad4b033e.jpg"); jsonObject.put("http://e.hiphotos.baidu.com/album/w%3D2048/sign=c54faa473ac79f3d8fe1e3308e99cc11/7a899e510fb30f244ed503bbc995d143ad4b033e.jpg"); for (int i = 0; i < 100; i++) { list.add(jsonObject); } } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return list.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if(convertView == null){ holder = new ViewHolder(); convertView = mInflater.inflate(R.layout.list_item_layout,null); holder.textView = (TextView)convertView; convertView.setTag(holder); }else{ holder = (ViewHolder)convertView.getTag(); } JSONArray array = list.get(position); SpannableStringBuilder builder = new SpannableStringBuilder(); holder.textView.setTag(R.id.listView1,builder); for (int i = 0; i < array.length(); i++) { try { String str = array.getString(i); //执行异步下载 mSpanAsyncLoad.displayImage(str, builder, holder.textView); } catch (JSONException e) { e.printStackTrace(); } } holder.textView.setText(builder); return convertView; } }
public static class ImageSpanAsyncLoad{ Context mContext; Drawable mDrawable = null;//默认显示图片 final LruCachedrawCache = new LruCache (100); public ImageSpanAsyncLoad(Context context) { mContext = context.getApplicationContext(); Resources resources = mContext.getResources(); mDrawable = resources.getDrawable(R.drawable.ic_launcher); mDrawable.setCallback(null); mDrawable.setBounds(0,0,mDrawable.getIntrinsicHeight(),mDrawable.getIntrinsicWidth()); } public void displayImage(String path,SpannableStringBuilder builder,TextView textView){ Drawable drawable = drawCache.get(path); boolean isNeedDown = false; if(drawable == null){//缓存无图片 isNeedDown = true; drawable = mDrawable; } ImageSpan imageSpan = new ImageSpan(drawable); SpannableString spannableString = new SpannableString(path); spannableString.setSpan(imageSpan,0,spannableString.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); builder.append(spannableString); if(isNeedDown){ int startIndex = builder.getSpanStart(imageSpan); int endIndex = builder.getSpanEnd(imageSpan); new AsyncTask