1. 程式人生 > >解決ListView中Item的子控制元件(比如Button)與Item點選事件衝突

解決ListView中Item的子控制元件(比如Button)與Item點選事件衝突

經常會碰到在ListView中點選其中一個Item,會一併觸發其子控制元件的點選事件,例如Item中的Button、ImageButton等,導致了點選Item中Button以外區域也會觸發Button點選事件。在網上找了相關方法,這裡記錄下,親測可行..
1、在Item的xml檔案根元素中新增屬性:android:descendantFocusability="blocksDescendants"

2、在衝突的子控制元件中新增屬性:android:focusable="false"android:clickable="true"