ǰÑÔ
ListViewÊÇAndroidÖÐ×î³£ÓõĿؼþ£¬Í¨¹ýÊÊÅäÆ÷À´½øÐÐÊý¾ÝÊÊÅäÈ»ºóÏÔʾ³öÀ´£¬¶øÆäÐÔÄÜÊǸöºÜÖµµÃÑо¿µÄ»°Ìâ¡£±¾ÎÄÓëÄãÒ»Æð̽ÌÖGoogle
I/OÌṩµÄÓÅ»¯Adapter·½°¸£¬»¶Ó´ó¼Ò½»Á÷¡£
ÕýÎÄ
Ò»¡¢×¼±¸
1.1ÁË½â¹ØÓÚGoogle IO´ó»á¹ØÓÚAdapterµÄÓÅ»¯£¬²Î¿¼ÒÔÏÂÎÄÕ£º
Android¿ª·¢Ö®ListView ÊÊÅäÆ÷£¨Adapter£©ÓÅ»¯
Android¿ª·¢¡ª¡ª09Google I/OÖ®ÈÃAndroid UIÐÔÄܸü¸ßЧ(1)
PDFÏÂÔØ£ºGoogle IO.pdf
1.2×¼±¸²âÊÔ´úÂ룺
Activity
private TestAdapter mAdapter;
private String[] mArrData;
private TextView mTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTV = (TextView) findViewById(R.id.tvShow);
mArrData = new String[1000];
for (int i = 0; i < 1000; i++) {
mArrData[i] = "Google IO Adapter" + i;
}
mAdapter = new TestAdapter(this, mArrData);
((ListView) findViewById(android.R.id.list)).setAdapter(mAdapter);
} |
´úÂë˵Ã÷£ºÄ£ÄâһǧÌõÊý¾Ý£¬TestAdapter¼Ì³Ð×ÔBaseAdapter£¬main.xml¼ûÎÄÕÂĩβÏÂÔØ¡£
¶þ¡¢²âÊÔ
²âÊÔ·½·¨£ºÊÖ¶¯»¬¶¯ListViewÖÁpositionÖÁ50È»ºóÍù»Ø»¬¶¯£¬³ä·ÖÀûÓÃconvertView²»µÈÓÚnullµÄ´úÂë¶Î¡£
2.1·½°¸Ò»
°´ÕÕGoogle I/O½éÉܵĵڶþÖÖ·½°¸£¬°Ñitem×ÓÔªËØ·Ö±ð¸ÄΪ4¸öºÍ10¸ö£¬ÕâÑùЧ¹û¸ü¼ÑÃ÷ÏÔ¡£
2.1.1²âÊÔ´úÂë
private int count = 0; private long sum = 0L; @Override public View getView(int position, View convertView, ViewGroup parent) { //¿ªÊ¼¼ÆÊ± long startTime = System.nanoTime(); if (convertView == null) { convertView = mInflater.inflate(R.layout.list_item_icon_text, null); } ((ImageView) convertView.findViewById(R.id.icon1)).setImageResource(R.drawable.icon); ((TextView) convertView.findViewById(R.id.text1)).setText(mData[position]); ((ImageView) convertView.findViewById(R.id.icon2)).setImageResource(R.drawable.icon); ((TextView) convertView.findViewById(R.id.text2)).setText(mData[position]); //Í£Ö¹¼ÆÊ± long endTime = System.nanoTime(); //¼ÆËãºÄʱ long val = (endTime - startTime) / 1000L; Log.e("Test", "Position:" + position + ":" + val); if (count < 100) { if (val < 1000L) { sum += val; count++; } } else mTV.setText(String.valueOf(sum / 100L));//ÏÔʾͳ¼Æ½á¹û return convertView; } |
2.1.2¡¡¡¡²âÊÔ½á¹û£¨Î¢Ãë³ýÒÔ1000£¬¼û´úÂ룩

2.2·½°¸¶þ
°´ÕÕGoogle I/O½éÉܵĵÚÈýÖÖ·½°¸£¬ÊǰÑitem×ÓÔªËØ·Ö±ð¸ÄΪ4¸öºÍ10¸ö¡£
2.2.1²âÊÔ´úÂë
private int count = 0;
private long sum = 0L;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// ¿ªÊ¼¼ÆÊ±
long startTime = System.nanoTime();
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_icon_text,
null);
holder = new ViewHolder();
holder.icon1 = (ImageView) convertView.findViewById(R.id.icon1);
holder.text1 = (TextView) convertView.findViewById(R.id.text1);
holder.icon2 = (ImageView) convertView.findViewById(R.id.icon2);
holder.text2 = (TextView) convertView.findViewById(R.id.text2);
convertView.setTag(holder);
}
else{
holder = (ViewHolder)convertView.getTag();
}
holder.icon1.setImageResource(R.drawable.icon);
holder.text1.setText(mData[position]);
holder.icon2 .setImageResource(R.drawable.icon);
holder.text2.setText(mData[position]);
// Í£Ö¹¼ÆÊ±
long endTime = System.nanoTime();
// ¼ÆËãºÄʱ
long val = (endTime - startTime) / 1000L;
Log.e("Test", "Position:" + position + ":" + val);
if (count < 100) {
if (val < 1000L) {
sum += val;
count++;
}
} else
mTV.setText(String.valueOf(sum / 100L));// ÏÔʾͳ¼Æ½á¹û
return convertView;
}
}
static class ViewHolder {
TextView text1;
ImageView icon1;
TextView text2;
ImageView icon2;
} |
2.2.2¡¡¡¡²âÊÔ½á¹û£¨Î¢Ãë³ýÒÔ1000£¬¼û´úÂ룩

2.3·½°¸Èý
´Ë·½°¸Îª¡°Henry Hu¡±Ìáʾ£¬API Level 4ÒÔÉÏÌṩ£¬ÕâÀï˳´ø²âÊÔÁËһϲ»Ê¹Óþ²Ì¬ÄÚ²¿ÀàÇé¿öÏÂÐÔÄÜ¡£
2.3.1²âÊÔ´úÂë
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// ¿ªÊ¼¼ÆÊ±
long startTime = System.nanoTime();
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_icon_text, null);
convertView.setTag(R.id.icon1, convertView.findViewById(R.id.icon1));
convertView.setTag(R.id.text1, convertView.findViewById(R.id.text1));
convertView.setTag(R.id.icon2, convertView.findViewById(R.id.icon2));
convertView.setTag(R.id.text2, convertView.findViewById(R.id.text2));
}
((ImageView) convertView.getTag(R.id.icon1)).setImageResource(R.drawable.icon);
((ImageView) convertView.getTag(R.id.icon2)).setImageResource(R.drawable.icon);
((TextView) convertView.getTag(R.id.text1)).setText(mData[position]);
((TextView) convertView.getTag(R.id.text2)).setText(mData[position]);
// Í£Ö¹¼ÆÊ±
long endTime = System.nanoTime();
// ¼ÆËãºÄʱ
long val = (endTime - startTime) / 1000L;
Log.e("Test", "Position:" + position + ":" + val);
if (count < 100) {
if (val < 1000L) {
sum += val;
count++;
}
} else
mTV.setText(String.valueOf(sum / 100L) + ":" + nullcount);// ÏÔʾͳ¼Æ½á¹û
return convertView;
} |
2.3.2²âÊÔ½á¹û£¨Î¢Ãë³ýÒÔ1000£¬¼û´úÂ룩
µÚÒ»´Î£º450
µÚ¶þ´Î£º467
µÚÈý´Î£º472
µÚËĴΣº451
µÚÎå´Î£º441
ËÄ¡¢×ܽá
4.1Ê×ÏÈÓÐÒ»¸öÈÏʶÊÇ´íÎóµÄ£¬ÎÒÃÇÏÈÀ´¿´½ØÍ¼£º

¿ÉÒÔ·¢ÏÖ£¬Ö»ÓеÚÒ»ÆÁ£¨¿ÉÊÓ·¶Î§£©µ÷ÓÃgetViewËùÏûºÄµÄʱ¼äÔ¶Ô¶¶àÓÚºóÃæµÄ£¬Í¨¹ý¶ÔconvertView
== nullÄÚ´úÂë¼à¿ØÒ²ÊÇͬÑùµÄ½á¹û¡£Ò²¾ÍÊÇ˵ListView½ö½ö»º´æÁË¿ÉÊÓ·¶Î§ÄÚµÄView£¬ËæºóµÄ¹ö¶¯¶¼ÊǶÔÕâЩView½øÐÐÊý¾Ý¸üС£²»¹ÜÄãÓжàÉÙÊý¾Ý£¬Ëû¶¼Ö»ÓÃArrayList»º´æ¿ÉÊÓ·¶Î§ÄÚµÄView£¬ÕâÑù±£Ö¤ÁËÐÔÄÜ£¬Ò²Ôì³ÉÁËÎÒÒÔΪListViewÖ»»º´æView½á¹¹²»»º´æÊý¾ÝµÄ¼ÙÏࣨ²»»áÖ»ÓÐÎÒÒ»ÈËÕâôÈÏΪ°É-
- #£©¡£ÕâÒ²ÄܽâÊÍΪʲôGOOGLEÓÅ»¯·½°¸Ò»±È¶þ¸ßºÜ¶àµÄÔÒò¡£ÄÇôʣϵÄÒ²¾ÍÖ»ÓÐfindViewById±È½ÏºÄʱÁË¡£¾Ý´Ë´ó¼Ò¿ÉÒÔ¿´¿´AbsListViewµÄÔ´´úÂ룬¿´¿´
obtainViewÕâ¸ö·½·¨ÄڵĴúÂë¼°RecycleBinÕâ¸öÀàµÄʵÏÖ£¬»¶Ó·ÖÏí¡£
´ËÍâÁ˽âÕâ¸öÔÀíÁË£¬ÄÇôÒÔÏ´úÂë²»ÔËÐÐÄã¿ÉÄܲµ½½á¹ûÁË£º
if (convertView == null) { convertView = mInflater.inflate(R.layout.list_item_icon_text, null); ((ImageView) convertView.findViewById(R.id.icon1)).setImageResource(R.drawable.icon); ((TextView) convertView.findViewById(R.id.text1)).setText(mData[position]); ((ImageView) convertView.findViewById(R.id.icon2)).setImageResource(R.drawable.icon); ((TextView) convertView.findViewById(R.id.text2)).setText(mData[position]); } else return convertView; |
û´í£¬Äã»á·¢ÏÖ¹ö¶¯Ê±»áÖØ¸´ÏÔʾµÚÒ»ÆÁµÄÊý¾Ý£¡
×ӿؼþÀïµÄʼþÒòΪÊÇͬһ¸ö¿Ø¼þ£¬Ò²¿ÉÒÔÖ±½Ó·Åµ½convertView ==
null ´úÂë¿éÄÚ²¿£¬Èç¹ûÐèÒª½»»¥Êý¾Ý±ÈÈçposition£¬¿ÉÒÔͨ¹ýtag·½Ê½À´ÉèÖò¢»ñÈ¡µ±Ç°Êý¾Ý¡£
4.2±¾ÎÄ·½°¸Ò»Óë·½°¸¶þ¶Ô±È
ÕâÀïÍÆ¼öÈç¹ûÖ»ÊÇÒ»°ãµÄÓ¦Óã¨Ò»°ãÖ¸×ӿؼþ²»¶à£©£¬ÎÞÐè¶¼ÊÇÓþ²Ì¬ÄÚ²¿ÀàÀ´ÓÅ»¯£¬Ê¹ÓõڶþÖÖ·½°¸¼´¿É£»·´Ö®£¬¶ÔÐÔÄÜÒªÇó½Ï¸ßʱ¿É²ÉÓᣴËÍâÐèÒªÌáÐѵÄÊÇÕâÀïÒ²ÊÇÓÿռ任ʱ¼äµÄ×ö·¨£¬View±¾ÉíÒòΪsetTag¶ø»áÕ¼Óøü¶àµÄÄڴ棬»¹»áÔö¼Ó´úÂëÁ¿£»¶øfindViewById»áÁÙʱÏûºÄ¸ü¶àµÄÄڴ棬ËùÒÔ²»¿ÉäĿʹÓã¬ÒÀʵ¼ÊÇé¿ö¶ø¶¨¡£
4.3·½°¸Èý
´Ë·½°¸Îª¡°Henry Hu¡±Ìáʾ£¬API Level 4ÒÔÉÏÖ§³Ö£¬ÔÀíºÍ·½°¸ÈýÒ»Ö£¬¼õÉÙfindViewById´ÎÊý£¬µ«ÊÇ´Ó²âÊÔ½á¹ûÀ´¿´Ð§¹û²¢²»ÀíÏ룬ÕâÀï²»ÔÙ×ö½øÒ»²½µÄ²âÊÔ¡£
½áÊø
¶ÔÓÚGoogle I/O´ó»áÕâ¸öÓÅ»¯·½°¸Ò»Ö±±§³ÙÒÉ̬¶È£¬´Ë·¬²âÊÔ×ÜËãÊÇÓÐÁ˸ü½øÒ»²½µÄÁ˽⣬»¶Ó´ó¼ÒÏȲâÊÔºó½»Á÷£¬¿´¿´»¹ÓÐʲô°ì·¨Äܹ»ÔÙÓÅ»¯Ò»µã¡£
Ò»¡¢ÐÂÀË΢²©
1.1½ØÍ¼

1.2·´±àÒëºóÏà¹Ø´úÂë
HomeListActivity
public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) { int i = --paramInt; int j = -1; if (i == j); for (Object localObject1 = HomeListActivity.this.getReloadView(); ;
localObject1 = HomeListActivity.this.getLoadMoreView()) { label26: return localObject1; int k = HomeListActivity.this.mList.size(); int l = paramInt; int i1 = k; if (l != i1) break; } boolean bool1 = true; boolean bool2 = null; String str1; label110: Object localObject2; if (StaticInfo.mUser == null) { List localList1 = HomeListActivity.this.mList; int i2 = paramInt; str1 = ((MBlog)localList1.get(i2)).uid; List localList2 = HomeListActivity.this.mList; int i3 = paramInt; String str2 = ((MBlog)localList2.get(i3)).uid; String str3 = str1; if (!str2.equals(str3)) break label271; int i4 = 1; label156: if (paramView != null) break label277; HomeListActivity localHomeListActivity1 = HomeListActivity.this; ListView localListView1 = HomeListActivity.this.mLvHome; List localList3 = HomeListActivity.this.mList; int i5 = paramInt; MBlog localMBlog1 = (MBlog)localList3.get(i5); HomeListActivity localHomeListActivity2 = HomeListActivity.this; int i6 = paramInt; boolean bool4 = localHomeListActivity2.isNewCommer(i6); int i7 = HomeListActivity.this.mReadMode; localObject2 = new MBlogListItemView(localHomeListActivity1,
localListView1, localMBlog1, bool1, bool2, i4, bool4, i7); } while (true) { localObject1 = localObject2; break label26: str1 = StaticInfo.mUser.uid; break label110: label271: boolean bool3 = null; break label156: label277: localObject2 = paramView; try { MainListItemView localMainListItemView = (MainListItemView)localObject2; List localList4 = HomeListActivity.this.mList; int i8 = paramInt; Object localObject3 = localList4.get(i8); HomeListActivity localHomeListActivity3 = HomeListActivity.this; int i9 = paramInt; boolean bool5 = localHomeListActivity3.isNewCommer(i9); int i10 = HomeListActivity.this.mReadMode; boolean bool6 = bool1; boolean bool7 = bool2; localMainListItemView.update(localObject3, bool6, bool7, bool5, i10); } catch (Exception localException) { HomeListActivity localHomeListActivity4 = HomeListActivity.this; ListView localListView2 = HomeListActivity.this.mLvHome; List localList5 = HomeListActivity.this.mList; int i11 = paramInt; MBlog localMBlog2 = (MBlog)localList5.get(i11); HomeListActivity localHomeListActivity5 = HomeListActivity.this; int i12 = paramInt; boolean bool8 = localHomeListActivity5.isNewCommer(i12); int i13 = HomeListActivity.this.mReadMode; localObject2 = new MBlogListItemView(localHomeListActivity4,
localListView2, localMBlog2, bool1, bool2, bool3, bool8, i13); } } } |
´úÂë˵Ã÷£º
´úÂëÁ÷³ÌÒѾ±È½Ï»ìÂÒ£¬µ«ÊÇÕâÀïÄÜ¿´µ½²¢Ã»ÓÐÖ±½ÓµÄinflate£¬¶øÊÇ×Ô¶¨ÒåÁ˼̳Ð×ÔLinearLayoutµÄMBlogListItemView¡£
MBlogListItemView
public MBlogListItemView(Context paramContext, ListView paramListView,
MBlog paramMBlog, boolean paramBoolean1, boolean paramBoolean2,
boolean paramBoolean3, boolean paramBoolean4, int paramInt) { super(paramContext); this.context = paramContext; this.parent = paramListView; this.mBlog = paramMBlog; String str1 = paramContext.getCacheDir().getAbsolutePath(); this.mCacheDir = str1; String str2 = paramContext.getFilesDir().getAbsolutePath(); this.mFileDir = str2; ((LayoutInflater)paramContext.getSystemService("layout_inflater")).inflate(2130903061, this); TextView localTextView1 = (TextView)findViewById(2131624016); this.mName = localTextView1; TextView localTextView2 = (TextView)findViewById(2131624041); this.mDate = localTextView2; TextView localTextView3 = (TextView)findViewById(2131624018); this.mContent = localTextView3; TextView localTextView4 = (TextView)findViewById(2131624046); this.mSubContent = localTextView4; ImageView localImageView1 = (ImageView)findViewById(2131624040); this.mIconV = localImageView1; ImageView localImageView2 = (ImageView)findViewById(2131624042); this.mIconPic = localImageView2; ImageView localImageView3 = (ImageView)findViewById(2131624044); this.mUploadPic1 = localImageView3; ImageView localImageView4 = (ImageView)findViewById(2131623979); this.mUploadPic2 = localImageView4; TextView localTextView5 = (TextView)findViewById(2131624047); this.tvForm = localTextView5; TextView localTextView6 = (TextView)findViewById(2131623989); this.tvComment = localTextView6; this.tvComment.setOnClickListener(this); TextView localTextView7 = (TextView)findViewById(2131623988); this.tvRedirect = localTextView7; this.tvRedirect.setOnClickListener(this); ImageView localImageView5 = (ImageView)findViewById(2131624049); this.imComment = localImageView5; this.imComment.setOnClickListener(this); ImageView localImageView6 = (ImageView)findViewById(2131624048); this.imRedirect = localImageView6; this.imRedirect.setOnClickListener(this); ImageView localImageView7 = (ImageView)findViewById(2131624043); this.imGpsIcon = localImageView7; ImageView localImageView8 = (ImageView)findViewById(2131624013); this.mPortrait = localImageView8; LinearLayout localLinearLayout = (LinearLayout)findViewById(2131624045); this.mSubLayout = localLinearLayout; this.mReadMode = paramInt; MBlogListItemView localMBlogListItemView = this; MBlog localMBlog = paramMBlog; boolean bool1 = paramBoolean1; boolean bool2 = paramBoolean2; boolean bool3 = paramBoolean4; int i = paramInt; localMBlogListItemView.update(localMBlog, bool1, bool2, bool3, i); this.mUploadPic1.setOnClickListener(this); this.mUploadPic2.setOnClickListener(this); }
¸´ÖÆ´úÂë |
´úÂë˵Ã÷£º
a).MBlogListItemView extends LinearLayout
implements MainListItemView
b).inflate(2130903061,this)Õâ¸öÊý×Ö´ú±íR.layout.itemview¡£
¶þ¡¢²âÊÔ·½°¸£¨·½°¸Î壩
°´ÕÕÐÂÀË΢²©ÀàËÆµÄ×ö·¨½øÐвâÊÔ¡£
2.1²âÊÔ´úÂë
@Override public View getView(int position, View convertView, ViewGroup parent) { // ¿ªÊ¼¼ÆÊ± long startTime = System.nanoTime();
TestItemLayout item;
if (convertView == null) {
item = new TestItemLayout(BaseAdapterActivity.this);
} else
item = (TestItemLayout) convertView;
item.icon1.setImageResource(R.drawable.icon);
item.text1.setText(mData[position]);
item.icon2.setImageResource(R.drawable.icon);
item.text2.setText(mData[position]);
// Í£Ö¹¼ÆÊ±
long endTime = System.nanoTime();
// ¼ÆËãºÄʱ
long val = (endTime - startTime) / 1000L;
Log.e("Test", "Position:"
+ position + ":" + val);
if (count < 100) {
if (val < 2000L) {
sum += val;
count++;
}
} else
mTV.setText(String.valueOf(sum / 100L) + ":"
+ nullcount);// ÏÔʾͳ¼Æ½á¹û
return item;
} |
TestItemLayout
public class TestItemLayout extends LinearLayout {
public TextView text1;
public ImageView icon1;
public TextView text2;
public ImageView icon2;
public TestItemLayout(Context context) {
super(context);
((LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
R.layout.list_item_icon_text, this);
icon1 = (ImageView) findViewById(R.id.icon1);
text1 = (TextView) findViewById(R.id.text1);
icon2 = (ImageView) findViewById(R.id.icon2);
text2 = (TextView) findViewById(R.id.text2);
}
} |
2.2²âÊÔ½á¹û

Èý¡¢×ܽá
´Ó²âÊÔ½á¹ûÀ´¿´ÓëViewHolderÐÔÄܷdz£½Ó½ü£¬²»»á³öÏÖtagͼƬ±äСµÄÎÊÌâ(¹ØÓÚͼƬ±äСµÄÎÊÌ⣬ÓÐÅóÓÑ˵ÊÇTAGÖеÄÔªËØ¶Ô´óСºÍλÖÃÓмÇÒä)£¬Ò²ÄÜÓÐЧµÄ¼õÉÙfindViewByIdµÄÖ´ÐдÎÊý£¬ÕâÀィÒéÍêÈ«¿ÉÒÔÈ¡´úViewHolder¡£
|