Android中Listview展示及其最佳化好處

來源:互聯網
上載者:User

標籤:

 

展示效果:

 

中間的item條目是可以上下滑動的。

 

代碼實現:

 1     @Override 2         public View getView(int position, View convertView, ViewGroup parent) { 3  4             ViewHolder holder; 5             if (convertView == null) { 6                 convertView = View.inflate(CleanCacheActivity.this, 7                         R.layout.list_item_cacheinfo, null); 8                 holder = new ViewHolder(); 9 10                 holder.tvName = (TextView) convertView11                         .findViewById(R.id.tv_name);12                 holder.tvCache = (TextView) convertView13                         .findViewById(R.id.tv_cache);14                 holder.ivIcon = (ImageView) convertView15                         .findViewById(R.id.iv_icon);16                 holder.ivClean = (ImageView) convertView17                         .findViewById(R.id.iv_clean);18 19                 convertView.setTag(holder);20             } else {21                 holder = (ViewHolder) convertView.getTag();22             }23             final CacheInfo info = getItem(position);24             holder.tvName.setText(info.name);25             holder.ivIcon.setImageDrawable(info.icon);
26        //......
27         return convertView;
28     }

 

 1     // listview 屬性封裝 2     class ViewHolder { 3         public TextView tvName; 4         public TextView tvCache; 5         public ImageView ivIcon; 6         public ImageView ivClean; 7     } 8  9     // 緩衝資訊封裝10     class CacheInfo {11         public String name;12         public String packageName;13         public Drawable icon;14         public long cacheSize;15     }

 

 

 

最佳化特點:

1.

  減少findViewById的次數

2.

  減少建立對象的次數(holder = new ViewHolder();)

3.

  實現分頁載入,節省消耗。

 

Android中Listview展示及其最佳化好處

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.