阅读344 返回首页    go 阿里云 go 技术社区[云栖]


Android: Gallery的adapter中getView方法被执行多次

项目中遇到一个奇怪的问题:为Gallery设置的adapter中的getView方法被调用多次

客户需求:一次只能滑动一格


参考资料:

https://stackoverflow.com/questions/4687370/gallery-scroll-one-image-at-a-time


https://stackoverflow.com/questions/6058609/android-gallerys-getview-returning-incorrect-position

Android Adapter multiple getView

https://stackoverflow.com/questions/3196219/android-adapter-multiple-getview


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

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

解决方案:
1:
在listview外面套一层RelativeLayout,将listview高度设置为fill_parent


<RelativeLayout xmlns:android="https://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="https://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>



当ListView\GridView设置固定大小,就不会多次调用了~



最后更新:2017-04-04 07:03:25

  上一篇:go 苹果公司面试11问
  下一篇:go linux shell 指令好记速查