1. 程式人生 > >android-繼承BaseAdapter--自定義介面卡,getView執行多次的解決方法

android-繼承BaseAdapter--自定義介面卡,getView執行多次的解決方法

定義的getView執行多次的ListView佈局:
 <ListView
        android:id="@+id/lv_messages"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/linearLayout1"
        android:cacheColorHint="#0000"
        android:divider="#0000"
        android:dividerHeight="2dp"
        android:listSelector="#0000" >
    </ListView>

執行多次原因是因為每顯示一個VIew,它都去測量view的高度,執行measure方法,導致getView執行多次。

正確的佈局:

 <ListView
        android:id="@+id/lv_messages"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/linearLayout1"
        android:cacheColorHint="#0000"
        android:divider="#0000"
        android:dividerHeight="2dp"
        android:listSelector="#0000" >
    </ListView>

把width和height都設定成fill_parent,如果ListView有父佈局,也把父佈局的width和height設定成fill_parent,這樣就成功的解決了自定義介面卡getView 執行多次的問題。

下面這3行可以解決ListView滑動變黑的問題:

   android:cacheColorHint="#0000"
        android:divider="#0000"
        android:listSelector="#0000" 

希望可以幫助到大家解決問題!