【轉】ListView中的BaseAdapter

本文轉自:http://blog.csdn.net/tianshuguang/article/details/7344315

重寫ListVIew的BaseAdapter需要重寫一下四個方法:

getCount()、getItem(int position)、getItemId(int position)以及getView(),其中最重要的是getView()方法。

@Override
public int getCount() {
    // How many items are in the data set represented by this Adapter.
  //(在此適配器中所代表的數據集中的條目數)
    return 0;
}

@Override
public Object getItem(int i) {
    // Get the data item associated with the specified position in the data set.
    //(獲取數據集中與指定索引對應的數據項)
    return null;
}

@Override
public long getItemId(int i) {
    // Get the row id associated with the specified position in the list.
    //(取在列表中與指定索引對應的行id)
    return 0;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    // Get a View that displays the data at the specified position in the data set.
    return null;
}

其中最重要的處理就是getView:

第一種:沒有任何處理。但是如果列表項數據量很大的時候,會每次都重新創建View,設置資源,嚴重影響性能。

@Override
public View getView(int i, 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,這種利用緩存contentView的方式可以判斷如果緩存中不存在View才創建View,如果已經存在可以利用緩存中的View,提升了性能。

@Override
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靜態類 
staticclass 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;
}

到這裏,可能會有人問ViewHolder靜態類結合緩存convertView與直接使用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())


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章