Android listview中使用button解決方案,androidlistview

來源:互聯網
上載者:User

Android listview中使用button解決方案,androidlistview

其實很多東西不是粘貼代碼就能解決的,做任何事都要用心做,這樣才能晚上睡覺睡得比較踏實.

當你粘貼別人的代碼,有時候出bug真的很煩,可是當你明白點原理的時候,能看出問題的實質,解決起來也很得心應手,開始今天的主題.當你的看到這個問題時,你應該知道問題出在哪裡了,在listview中使用button(當然不只是button,還有imagebutton等自身響應點擊的view),問題出現了,你會發現listview中的item不能點擊了,具體是因為什麼呢?大多數原因是搶焦點,button搶佔了item的焦點使得item無法響應點擊事件,那如何解決呢?今天的第一個主角(descendantFocusability)就要閃亮登場了...

android:descendantFocusability有三個值,分別是:beforeDescendants,afterDescendants,blocksDescendants

1.android:descendantFocusability="beforeDescendants"這句的意思就是說listview的item先拿到焦點

2.android:descendantFocusability="afterDescendants"只有button不需要焦點時item才能拿到焦點

3.android:descendantFocusability="blocksDescendants"item覆蓋button直接拿到焦點

看到這三條屬性的作用你應該知道怎麼解決了吧,listview和button出現搶焦點時我一般使用使用第三條,然後在adapter寫button的onclick事件,這樣大家誰都不惹誰,和平相處.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.