Android中最佳化ListView

來源:互聯網
上載者:User

最佳化ListView,可以最佳化適配器,在複寫public View getView(int
position, View convertView, ViewGroupparent)的方法時候,要複用convertView這個“曆史”View對象。代碼:

 

public class Appmanaget_adapterextends BaseAdapter
{

    private List<App>lis;//需要在listview中顯示的資料的List的APP實體類集合集合。

                                 //App中包好一個Drawable類型的icon和一個String類型的appName的屬性

    private Contextcontext;

    //填充item的資源檔中包含ImageView,和TextView兩控制項

    //將要在listview的item中顯示的控制項聲明成靜態,減少運行時ImageView,ImageView執行個體

    privatestatic ImageViewiv;

    privatestatic TextViewtv;

    public Appmanaget_adapter(List<App> lis,Context context){

       super();

       this.lis = lis;

       this.context=context;

    }

 

    @Override

    publicint getCount() {

       returnlis.size();

    }

 

    @Override

    public Object getItem(int position) {

       returnlis.get(position);

    }

 

    @Override

    publiclong getItemId(int
position) {

       return position;

    }

    @Override

    public View getView(int position, View convertView,
ViewGroupparent) {

       View v = null;

       //convertView可以理解為一個曆史的view控制項

       if(convertView==null){

           //如果convertView為null,就要對v進行初始化,View.inflate(context,//R.layout.adaper_appmanager,
null);

           //這個需要消耗比較大的時間

           v=View.inflate(context, R.layout.adaper_appmanager,null);

       }else{

           //如果convertView不為null,說明有一個Item被使用者滾動出的螢幕,這個時候要顯示新的item就可以複用這個convertView了,

           //就不用再進行View.inflate,在listview要顯示比較多的item時候,就能提高很大效率

           v=convertView;

       }

       iv=(ImageView) v.findViewById(R.id.adapter_appmanager_iv_appIcon);

       tv=(TextView) v.findViewById(R.id.adapter_appmanager_tv_appName);

       iv.setImageDrawable(lis.get(position).getIcon());

       tv.setText(lis.get(position).getAppName());

       return v;

    }

 

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.