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

简介:

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

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


参考资料:

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


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

Android Adapter multiple getView

http://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="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>



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



相关文章
|
8天前
|
机器学习/深度学习 Java Shell
[RK3568][Android12.0]--- 系统自带预置第三方APK方法
[RK3568][Android12.0]--- 系统自带预置第三方APK方法
51 0
|
7天前
|
Android开发
Android获取蓝牙设备列表的方法
Android获取蓝牙设备列表的方法
16 5
|
7天前
|
Android开发
Android获取当前系统日期和时间的三种方法
Android获取当前系统日期和时间的三种方法
17 4
|
8天前
|
API 开发工具 Android开发
调用Android原生@SystemApi、@Hide方法
调用Android原生@SystemApi、@Hide方法
11 1
|
8天前
|
程序员 Android开发
Android亮度调节的几种实现方法
Android亮度调节的几种实现方法
12 0
|
8天前
|
Shell Android开发
Android Activity重写dump方法实现通过adb调试代码
Android Activity重写dump方法实现通过adb调试代码
17 0
|
8天前
|
Android开发
Android APP 隐藏系统软键盘的方法
Android APP 隐藏系统软键盘的方法
40 0
|
8天前
|
Android开发
Android中去掉ActionBar的几种方法
Android中去掉ActionBar的几种方法
15 0
|
8天前
|
安全 Java Shell
Android11以上 Audio音频调试方法
Android11以上 Audio音频调试方法
79 0
|
8天前
|
Android开发
Android调用相机与相册的方法2
Android调用相机与相册的方法
25 0