閱讀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 指令好記速查