且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

android listview中getView重复被调用的问题

更新时间:2022-08-18 14:32:07

getView被调用的次数取决于当前屏幕能显示的item的数量,显示一个item就调用一次。当listview的layout_height属性是wrap_content时,有可能造成listview无法计算高度,相应的需要显示的item数量就无法计算,造成getView多次被调用。

解决方案:
1:
在listview外面套一层RelativeLayout,将listview高度设置为fill_parent
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="@drawable/repeat_bg">

 <ListView

            android:id="@+id/friend_list"
            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:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#f3f3f3"
    android:orientation="vertical" >

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

</LinearLayout>