簡體   English   中英

了解convertView

[英]Understanding convertView

我試圖了解convertView如何工作。 我確實閱讀了我遇到的大多數文檔,並對StackOverflow提出了疑問。 我以為我已經了解了它的工作原理,但是當涉及到實現時,我做得不好。

截至目前我的代碼:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if(Const.DEBUGGING){
        Log.d(Const.DEBUG, "Position = "+position);
    }

    if (convertView == null) {

        if(Const.DEBUGGING){
            Log.d(Const.DEBUG, "convertView is NULL");
        }

        convertView = getActivity().getLayoutInflater().inflate(
                R.layout.item_mtf_results, parent, false);

        holder = new ViewHolder();
        holder.txtViewResults = (TextView) convertView
                .findViewById(R.id.textview_item_mtf_results);

        convertView.setTag(holder);
    } else {

        if(Const.DEBUGGING){
            Log.d(Const.DEBUG, "convertView is NOT Null");
        }

        holder = (ViewHolder) convertView.getTag();
    }

    holder.txtViewResults.setText(dummyText[position]);

    return convertView;
}

我的Logcat上面的代碼:

03-30 10:27:29.433: D/TYM(29043): Position = 0
03-30 10:27:29.433: D/TYM(29043): convertView is NULL
03-30 10:27:29.433: D/TYM(29043): Position = 1
03-30 10:27:29.433: D/TYM(29043): convertView is NOT Null
03-30 10:27:29.433: D/TYM(29043): Position = 2
03-30 10:27:29.433: D/TYM(29043): convertView is NOT Null
03-30 10:27:29.433: D/TYM(29043): Position = 3
03-30 10:27:29.433: D/TYM(29043): convertView is NOT Null
03-30 10:27:29.433: D/TYM(29043): Position = 4
03-30 10:27:29.433: D/TYM(29043): convertView is NOT Null
03-30 10:27:29.433: D/TYM(29043): Position = 5
03-30 10:27:29.433: D/TYM(29043): convertView is NOT Null
03-30 10:27:29.433: D/TYM(29043): Position = 6
03-30 10:27:29.433: D/TYM(29043): convertView is NOT Null
03-30 10:27:29.433: D/TYM(29043): Position = 7
03-30 10:27:29.433: D/TYM(29043): convertView is NOT Null
03-30 10:27:29.433: D/TYM(29043): Position = 8
03-30 10:27:29.433: D/TYM(29043): convertView is NOT Null
03-30 10:27:29.453: D/TYM(29043): Position = 0
03-30 10:27:29.453: D/TYM(29043): convertView is NOT Null
03-30 10:27:29.453: D/TYM(29043): Position = 1
03-30 10:27:29.453: D/TYM(29043): convertView is NULL
03-30 10:27:29.453: D/TYM(29043): Position = 2
03-30 10:27:29.453: D/TYM(29043): convertView is NULL
03-30 10:27:29.453: D/TYM(29043): Position = 3
03-30 10:27:29.453: D/TYM(29043): convertView is NULL
03-30 10:27:29.453: D/TYM(29043): Position = 4
03-30 10:27:29.453: D/TYM(29043): convertView is NULL
03-30 10:27:29.463: D/TYM(29043): Position = 5
03-30 10:27:29.463: D/TYM(29043): convertView is NULL
03-30 10:27:29.463: D/TYM(29043): Position = 6
03-30 10:27:29.463: D/TYM(29043): convertView is NULL
03-30 10:27:29.463: D/TYM(29043): Position = 7
03-30 10:27:29.463: D/TYM(29043): convertView is NULL
03-30 10:27:29.463: D/TYM(29043): Position = 8
03-30 10:27:29.463: D/TYM(29043): convertView is NULL
03-30 10:27:29.503: D/TYM(29043): Position = 0
03-30 10:27:29.503: D/TYM(29043): convertView is NULL
03-30 10:27:29.503: D/TYM(29043): Position = 1
03-30 10:27:29.503: D/TYM(29043): convertView is NOT Null
03-30 10:27:29.503: D/TYM(29043): Position = 2
03-30 10:27:29.503: D/TYM(29043): convertView is NOT Null
03-30 10:27:29.503: D/TYM(29043): Position = 3
03-30 10:27:29.503: D/TYM(29043): convertView is NOT Null
03-30 10:27:29.503: D/TYM(29043): Position = 4
03-30 10:27:29.503: D/TYM(29043): convertView is NOT Null
03-30 10:27:29.503: D/TYM(29043): Position = 5
03-30 10:27:29.503: D/TYM(29043): convertView is NOT Null
03-30 10:27:29.503: D/TYM(29043): Position = 6
03-30 10:27:29.503: D/TYM(29043): convertView is NOT Null
03-30 10:27:29.503: D/TYM(29043): Position = 7
03-30 10:27:29.503: D/TYM(29043): convertView is NOT Null
03-30 10:27:29.503: D/TYM(29043): Position = 8
03-30 10:27:29.503: D/TYM(29043): convertView is NOT Null

截圖:

在此處輸入圖片說明

據我對convertView的了解,第一次在ListView中加載項目時,內存僅分配給屏幕中加載的那些視圖,另外一些則分配給緩沖區。 當我們開始滾動時,離開屏幕的視圖將被回收為現有視圖。

因此,當屏幕第一次加載時,convertView將為NULL,因此將分配內存。 僅當我們開始滾動並且視圖可用於回收時,convertView才不會為Null。 這是正確的嗎?

上面的Logcat第一次顯示加載屏幕時的日志。

我的問題:

  1. 為什么有3個調用用於所有視圖。 剛剛第一次加載了屏幕。
  2. 為什么convertView在第二個位置不是NULL?
  3. 為什么在第二個循環中convertView再次為NULL?

為了更好地了解運行情況,請將調試器斷點放在getView()並檢查調用堆棧跟蹤以查看為什么進行了調用。

您的ListView似乎是一個復雜的布局,需要多次測量/布局。 例如,具有權重的LinearLayout或具有子級之間依賴關系的RelativeLayout

要測量ListView ,必須測量可見的子項。 這就解釋了對getView()這種調用,對於兩次通過的布局,解釋了兩個。 同樣,出於測量目的,視圖可以立即回收,因為它們不需要在屏幕上顯示。

道德:避免將適配器視圖放置在多遍布局中。 如果可能,也應避免使用多遍布局。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM