我已閱讀了一些文章,並觀看了video以瞭解ListView
的工作原理。所以,假設我有5個項目的列表(所有項目都可以在屏幕上看到)。我在我的自定義適配器下面的代碼從BaseAdapter
繼承:android getView()如何在BaseAdapter中工作?
@Override
public View getView(int position, View view, ViewGroup parent) {
System.out.println("item position: " + position + " view " + view);
if (view == null)
{
view = LayoutInflater.from(context).inflate(R.layout.list_view_item, null);
}
return view;
}
當我運行此代碼,我看到控制檯輸出如下:
08-26 14:52:40.882: item position: 0 view null
08-26 14:52:40.883: item position: 1 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.883: item position: 2 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 3 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 4 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 0 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 1 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 2 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 3 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 4 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 0 view android.widget.RelativeLayout{88bc756 V.E...C.. ......I. 0,0-1080,65 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 1 view android.widget.RelativeLayout{1264e60 V.E...C.. ......I. 0,68-1080,133 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 2 view android.widget.RelativeLayout{36b089 V.E...C.. ......I. 0,136-1080,201 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 3 view android.widget.RelativeLayout{e1a12bb V.E...C.. ......I. 0,204-1080,269 #7f0e00b8 app:id/item_container}
08-26 14:52:40.885: item position: 4 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
我getView()
view
是null
只爲0
位置。看完後,我認爲我有null
前5個電話(直到所有可見的項目視圖未初始化)。爲什麼只有第一個?我錯過了什麼?
是否可以發佈您的list_view_item代碼 – Ashish
在我的環境中,視圖全爲空。你能發佈完整的佈局和適配器代碼嗎? – nshmura