Android GridView中Button點擊事件onItemClick不能響應

來源:互聯網
上載者:User

標籤:

今天在搗鼓一個類似於百度貼吧的東西。布局:上面是個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不能響應

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.