Android基礎入門教程——2.4.3 BaseAdapter最佳化

來源:互聯網
上載者:User

Android基礎入門教程——2.4.3 BaseAdapter最佳化
Android基礎入門教程——2.4.3 BaseAdapter最佳化

標籤(空格分隔): Android基礎入門教程

本節引言:

上一節中我們學習了如何來使用一個ListView以及自訂一個簡單的BaseAdapter,我們從代碼
中可以看出比較重要的兩個方法:getCount()和getView(),介面上有多少列就會調用多少次getView,
這個時候可能看出一些端倪,每次都是新inflate一個View,都要進行這個XML的解析,這樣會
很浪費資源,當然,幾十列或者幾百列的列表並不能體現什麼問題,但假如更多或者布局更加複雜?
所以學習ListView的最佳化很重要,而本節針對的是BaseAdapter的最佳化,最佳化的兩點有,複用convertView
以及使用ViewHolder重用組件,不用每次都findViewById,我們具體通過代碼來體會吧!

1.複用ConvertView:

上面也說了,介面上有多少個Item,那麼getView方法就會被調用多少次!
我們來看看上一節我們寫的getView()部分的代碼:

  @Override    public View getView(int position, View convertView, ViewGroup parent) {        convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list_animal,parent,false);        ImageView img_icon = (ImageView) convertView.findViewById(R.id.img_icon);        TextView txt_aName = (TextView) convertView.findViewById(R.id.txt_aName);        TextView txt_aSpeak = (TextView) convertView.findViewById(R.id.txt_aSpeak);        img_icon.setBackgroundResource(mData.get(position).getaIcon());        txt_aName.setText(mData.get(position).getaName());        txt_aSpeak.setText(mData.get(position).getaSpeak());        return convertView;    }

是吧,inflate()每次都要載入一次xml,其實這個convertView是系統提供給我們的可供服用的View
的緩衝對象,那就坐下判斷咯,修改下,最佳化後的代碼:

 @Override    public View getView(int position, View convertView, ViewGroup parent) {        if(convertView == null){            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list_animal,parent,false);        }        ImageView img_icon = (ImageView) convertView.findViewById(R.id.img_icon);        TextView txt_aName = (TextView) convertView.findViewById(R.id.txt_aName);        TextView txt_aSpeak = (TextView) convertView.findViewById(R.id.txt_aSpeak);        img_icon.setBackgroundResource(mData.get(position).getaIcon());        txt_aName.setText(mData.get(position).getaName());        txt_aSpeak.setText(mData.get(position).getaSpeak());        return convertView;    }
2.ViewHolder重用組件

嘿嘿,getView()會被調用多次,那麼findViewById不一樣得調用多次,而我們的ListView的Item
一般都是一樣的布局,我們可以對這裡在最佳化下,我們可以自己定義一個ViewHolder類來對這一部分
進行效能最佳化!修改後的代碼如下:

@Override    public View getView(int position, View convertView, ViewGroup parent) {        ViewHolder holder = null;        if(convertView == null){            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list_animal,parent,false);            holder = new ViewHolder();            holder.img_icon = (ImageView) convertView.findViewById(R.id.img_icon);            holder.txt_aName = (TextView) convertView.findViewById(R.id.txt_aName);            holder.txt_aSpeak = (TextView) convertView.findViewById(R.id.txt_aSpeak);            convertView.setTag(holder);   //將Holder儲存到convertView中        }else{            holder = (ViewHolder) convertView.getTag();        }        holder.img_icon.setBackgroundResource(mData.get(position).getaIcon());        holder.txt_aName.setText(mData.get(position).getaName());        holder.txt_aSpeak.setText(mData.get(position).getaSpeak());        return convertView;    }    static class ViewHolder{        ImageView img_icon;        TextView txt_aName;        TextView txt_aSpeak;    }

沒錯就是這麼簡單,你以後BaseAdapter照著這個模板寫就對了,哈哈,另外這個修飾ViewHolder的
static你可以看情況改成其他的,如果條目較少的話,可以使用static,如果條目多的花,還是別
定義成靜態~

本節小結:

好的,關於BaseAdapter的最佳化大概就上述的兩種,非常簡單,複用ConvertView以及自訂ViewHolder
減少findViewById()的調用~如果你有其他關於BaseAdapter最佳化的建議歡迎提出,謝謝~

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

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