這個文章純屬是從自己多次摔倒的經歷中讓我不得不整理起來。其實,感覺每次都不知道怎么解決這個問題,但是,下次遇到的時候還是有被卡住了,我相信這里還有很多人跟我一樣。
其實在Android中,這個問題是很常見的,但是,還是有很多沒有的到最根本的解決。
我不是一個理論知識家,只是記下我覺得需要記下的東西,當然,這里我肯定希望能幫到更多的人。謝謝
EG:
這里有一個xml寫的是需要填充內容的listview或者gridview(A),然后,肯定這里還會有另外一個xml用來布局listview里面的樣式。我一般以cell結尾(B)。
這里,你會發現如果在B里面我們有獲得焦點的控件的時候,對於A里面的控件在實現onitemclicklistener的時候,根本沒有執行。
其實,是因為他的點擊事件已經被他自己布局里面的控件給獲取了,所以,我們應該在B的大布局里面加上: android:descendantFocusability="blocksDescendants",
beforeDescendants:viewgroup會優先其子類控件而獲取到焦點
afterDescendants:viewgroup只有當其子類控件不需要獲取焦點時才獲取焦點
blocksDescendants:viewgroup會覆蓋子類控件而直接獲得焦點
然后,在不想他搶走焦點的控件上面寫好這兩句就okay:
android:clickable="true"
android:focusable="false"
其它的跟Listview同風格的照做就好:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:lhl="http://schemas.android.com/apk/res/com.childhos.childhos" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/layout_game_list" android:layout_width="match_parent" android:layout_height="match_parent" android:descendantFocusability="blocksDescendants" android:orientation="horizontal" > <com.lhl.view.RoundImageView android:id="@+id/image_game" android:layout_width="@dimen/btn_one_height" android:layout_height="@dimen/btn_one_height" android:layout_gravity="center" android:layout_margin="10dip" android:src="@drawable/image_game_one" lhl:borderRadius="15dp" lhl:type="round" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="vertical" > <TextView android:id="@+id/text_name" style="@style/TextStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000" android:textSize="@dimen/text_four" /> <TextView android:id="@+id/text_down_time" style="@style/TextStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000" android:textSize="@dimen/text_four" /> <TextView android:id="@+id/text_app_size" style="@style/TextStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000" android:textSize="@dimen/text_four" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right|center_vertical" android:layout_weight="1" android:gravity="right|center_vertical" android:orientation="vertical" > <TextView android:id="@+id/btn_install" android:layout_width="@dimen/btn_one_height" android:layout_height="30dip" android:layout_gravity="right|center_vertical" android:layout_margin="10dip" android:background="@drawable/border_sel" android:clickable="true" android:focusable="false" android:gravity="center" android:text="安裝" android:textColor="#398de3" android:textSize="@dimen/text_three" /> </LinearLayout> </LinearLayout>
謝謝!紅牛,加油!