【 Android 】Listview 使用 BaseAdapter 並自訂 ImageButton

目的 : 再專案上使建立listview,並使用base adapter自訂view,同時在每個row上加上imagebutton。

問題 : imagebutton時有時無的反應。

原因 : 由於此專案需要一直更新listview上的資訊,所以在使用上會定時的更新( setListAdapter(adapter);),而每更新一次則會掃一次listview有多少row,而listview上的imagebutton的監聽是在 adapter裡的getView裡,所以會一直被reset而造成按鍵無反應的狀態。

解決方式:
1.   待row上的資訊與新的資訊不符合時,再下setListAdapter(adapter);去更新listview。
2. 在整個佈局加上     
            android:descendantFocusability="blocksDescendants"
3. 在imagebutton上加上  
            android:focusable="false"
            android:focusableInTouchMode="false"

這樣imagebutton應該就可以很順暢的操作了。

留言

這個網誌中的熱門文章

【 Android 】控制ImageView大小&位置

【 Android 】利用file取得資料夾內檔案

【 Android 】防止系統進入休眠和螢幕恆亮