zoukankan      html  css  js  c++  java
  • Android中Listview展示及其优化好处

    展示效果:

    中间的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) convertView
    11                         .findViewById(R.id.tv_name);
    12                 holder.tvCache = (TextView) convertView
    13                         .findViewById(R.id.tv_cache);
    14                 holder.ivIcon = (ImageView) convertView
    15                         .findViewById(R.id.iv_icon);
    16                 holder.ivClean = (ImageView) convertView
    17                         .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.

      实现分页加载,节省消耗。

  • 相关阅读:

    队列
    Collection类
    Hashtable类、IdentityHashMap和WeakHashMap类
    LinkedHashMap类
    广播的种类:有序广播和无序广播
    自定义的BroadCastReceiver
    String的两个API,判断指定字符串是否包含另一字符串,在字符串中删除指定字符串。
    BroadcastReceiver的最简单用法
    Notification通知栏
  • 原文地址:https://www.cnblogs.com/rongsnow/p/5402941.html
Copyright © 2011-2022 走看看