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>