編者:李國帥
qq:9611153 微信lgs9611153
時間:2020-04-10
背景原因:
在android應用程序開發中,列表是常見的界面元素,然而,經常會遇到列表的子項無法選中的現象。
列表的選項即應該靈敏,也要避免重複點擊造成的錯誤。
這個要分情況:
如果是點擊之後需要和後臺互動,或者需要進行跳轉,那麼就需要避免多次快速點擊。
如果僅僅時應用程序內部數據或者界面操作,那麼就需要及時反應,避免影響用戶操作,給人反應遲緩的印象。
下面遇到的就是僅僅本地操作時,點擊事件不能馬上反應的問題。遇到了以下這個問題。
解決方法:
常會遇到這種情況,RecyclerView或者ListView中的子項list_item_xxx.xml,觸發點擊整行事件時,當點擊checkbox區域可能無法觸發點擊整行事件。
應該是checkbox點擊事件阻擋了點擊整行事件,其實edittext,button,radiobox這些控件也是可能影響到整行點擊事件的。
要解決,只需要把CheckBox更改爲ImageView,自己控制view的顯示樣式即可,就不會影響到點擊事件了。
下面就是解決的實例。
原本代碼:
item處理部分
list_item.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/C_FFFFFF"
android:orientation="vertical">
<LinearLayout
android:id="@+id/ll_item_list_main"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_100"
android:layout_marginLeft="@dimen/dp15"
android:background="@color/C_FFFFFF"
android:gravity="center_vertical"
android:orientation="horizontal">
<CheckBox
android:id="@+id/cb_item_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:button="@drawable/checkbox_selector"
android:enabled="false"
android:focusable="false" />
更改爲:
item處理部分
list_item.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/C_FFFFFF"
android:orientation="vertical">
<LinearLayout
android:id="@+id/ll_item_list_main"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_100"
android:layout_marginLeft="@dimen/dp15"
android:background="@color/C_FFFFFF"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_item_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:src="@drawable/check_box_nor"
android:focusable="false" />
點擊回調時手動處理顯示狀態:
如此便不會出現無法選中列表項的情況。