標籤:
今天在搗鼓一個類似於百度貼吧的東西。布局:上面是個ActionBar標題列,然後是一個GridView布局,在Java代碼中動態載入關注的貼吧,一切就緒中,很愉快的弄好了!
現在需要點擊選項進入某個貼吧,那麼問題來了—— GridView中Button點擊事件onItemClick不能響應。
所以,主要的陷阱還是在com.android.internal.R.attr.buttonStyle這個裡面,查看這個xml檔案,Button設定多了兩個屬性如下:
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
所以我們要在代碼裡面把這兩個屬性設為false,這樣就可以響應GridView的onItemClick方法了,但是一定要注意,不管是Button,還是TextView,只要設定了onClick() 的話,那麼OnItemClick()就不會被執行(以上的內容都是基於一個GridView或者是ListView的每個Item只有一項,要不是Button,要不是TextView,不是這種情況的,上面的就只能參考一下了)。
解決方案:(修改Button XML布局)加上如下兩個就ok了……
android:focusable="false"
android:clickable="false"
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:focusable="false" android:clickable="false" android:id="@+id/btn_spot" android:text="" /></LinearLayout>
參考文獻:zgz345部落格園,Android GridView中設定了Button以後就不能響應OnItemClick(),http://www.cnblogs.com/zgz345/archive/2012/07/05/2578110.html
Android GridView中Button點擊事件onItemClick不能響應