閱讀445 返回首頁    go 阿裏雲 go 技術社區[雲棲]


android listview中getView重複被調用的問題

getView被調用的次數取決於當前屏幕能顯示的item的數量,顯示一個item就調用一次。當listview的layout_height屬性是wrap_content時,有可能造成listview無法計算高度,相應的需要顯示的item數量就無法計算,造成getView多次被調用。

解決方案:
1:
在listview外麵套一層RelativeLayout,將listview高度設置為fill_parent
<RelativeLayout xmlns:andro
    android:
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="@drawable/repeat_bg">

 <ListView

            android:
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"

            android:layout_weight="1"
            android:cacheColorHint="#00000000"
            android:divider="#CCCCCC"
            android:fastScrollEnabled="true"
            android:focusableInTouchMode="true" />


</RelativeLayout>

2:在listview外麵套一層 LinearLayout,將listview高度設置為0dip

<LinearLayout xmlns:andro
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#f3f3f3"
    android:orientation="vertical" >

     <ListView
        android:
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:cacheColorHint="#00000000"
        android:divider="#CCCCCC" />

</LinearLayout>


最後更新:2017-04-04 07:03:06

  上一篇:go 解決linux下無法添加用戶和組的問題(groupadd命令不可用)
  下一篇:go Android開發21——隱式意圖