Android -- 重寫BaseAdapter以及對ListView的最佳化

來源:互聯網
上載者:User

標籤:android   style   blog   color   使用   os   io   strong   

背景                                                                                   

對於ListView、GridView、Gallery、Spinner等等,它是它們的適配器,直接繼承自介面類Adapter的,使用BaseAdapter時需要重寫很多方法,其中最重要的當屬getView,因為這會涉及到ListView最佳化等問題,BaseAdapter與其他Adapter有些不一樣,其他的Adapter可以直接在其構造方法中進行資料的設定:

SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.list_item, new String[]{"img","title","info",new int[]{R.id.img, R.id.title, R.id.info}});

但是在BaseAdapter中需要實現一個繼承自BaseAdapter的類,並且重寫裡面的很多方法:

class MyAdapter extends BaseAdapter    {        private Context context;        public MyAdapter(Context context)        {            this.context = context;        }        @Override        public int getCount() {            // How many items are in the data set represented by this Adapter.(在此適配器中所代表的資料集中的條目數)            return 0;        }        @Override        public Object getItem(int position) {            // Get the data item associated with the specified position in the data set.(擷取資料集中與指定索引對應的資料項目)            return null;        }        @Override        public long getItemId(int position) {            // Get the row id associated with the specified position in the list.(取在列表中與指定索引對應的行id)            return 0;        }        @Override        public View getView(int position, View convertView, ViewGroup parent) {            // Get a View that displays the data at the specified position in the data set.            return null;        }            }
沒有任何處理,不建議這樣寫。                                                     

如果資料量少看將就,但是如果清單項目資料量很大的時候,會每次都重新建立View,設定資源,嚴重影響效能,所以從一開始就不要用這種方式。

@Override        public View getView(int position, View convertView, ViewGroup parent) {            View item = mInflater.inflate(R.layout.list_item, null);            ImageView img = (ImageView)item.findViewById(R.id.img)             TextView title = (TextView)item.findViewById(R.id.title);            TextView info = (TextView)item.findViewById(R.id.info);            img.setImageResource(R.drawable.ic_launcher);            title.setText("Hello");            info.setText("world");                        return item;        }
ListView最佳化                                                                          

通過緩衝convertView,這種利用緩衝contentView的方式可以判斷如果緩衝中不存在View才建立View,如果已經存在可以利用緩衝中的View,提升了效能。

public View getView(int position, View convertView, ViewGroup parent) {            if(convertView == null)            {                convertView = mInflater.inflate(R.layout.list_item, null);            }                        ImageView img = (ImageView)convertView.findViewById(R.id.img)             TextView title = (TextView)convertView.findViewById(R.id.title);            TextView info = (TextView)ConvertView.findViewById(R.id.info);            img.setImageResource(R.drawable.ic_launcher);            title.setText("Hello");            info.setText("world");                        return convertView;        }
ListView再最佳化                                                                       

通過convertView+ViewHolder來實現,ViewHolder就是一個靜態類,使用 ViewHolder 的關鍵好處是緩衝了顯示資料的視圖(View),加快了 UI 的響應速度。

當我們判斷 convertView == null  的時候,如果為空白,就會根據設計好的List的Item布局(XML),來為convertView賦值,並產生一個viewHolder來綁定converView裡面的各個View控制項(XML布局裡面的那些控制項)。再用convertView的setTag將viewHolder設定到Tag中,以便系統第二次繪製ListView時從Tag中取出。(看下面代碼中)

如果convertView不為空白的時候,就會直接用convertView的getTag(),來獲得一個ViewHolder。

//在外面先定義,ViewHolder靜態類    static class ViewHolder    {        public ImageView img;        public TextView title;        public TextView info;    }//然後重寫getView        @Override        public View getView(int position, View convertView, ViewGroup parent) {            ViewHolder holder;            if(convertView == null)            {                holder = new ViewHolder();                convertView = mInflater.inflate(R.layout.list_item, null);                holder.img = (ImageView)item.findViewById(R.id.img)                 holder.title = (TextView)item.findViewById(R.id.title);                holder.info = (TextView)item.findViewById(R.id.info);                convertView.setTag(holder);            }else            {                holder = (ViewHolder)convertView.getTag();                holder.img.setImageResource(R.drawable.ic_launcher);                holder.title.setText("Hello");                holder.info.setText("World");            }                        return convertView;        }
  • 提升Adapter的兩種方法

To work efficiently the adapter implemented here uses two techniques:
-It reuses the convertView passed to getView() to avoid inflating View when it is not necessary

(譯:重用緩衝convertView傳遞給getView()方法來避免填充不必要的視圖)
-It uses the ViewHolder pattern to avoid calling findViewById() when it is not necessary

  • (譯:使用ViewHolder模式來避免沒有必要的調用findViewById():因為太多的findViewById也會影響效能)
    ViewHolder類的作用
    -The ViewHolder pattern consists in storing a data structure in the tag of the view returned by getView().This data structures contains references to the views we want to bind data to, thus avoiding calling to findViewById() every time getView() is invoked

(譯:ViewHolder模式通過getView()方法返回的視圖的標籤(Tag)中儲存一個資料結構,這個資料結構包含了指向我們要綁定資料的視圖的引用,從而避免每次調用getView()的時候調用findViewById())

我是天王蓋地虎的分割線                                                             

聯繫我們

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