阅读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——隐式意图