android:descendantFocusability用法

此屬性一般解決lisview中嵌套按鈕而出現的點擊時間衝突問題。
官方文檔描述如下:
android:descendantFocusability

Defines the relationship between the ViewGroup and its descendants when looking for a View to take focus.

Must be one of the following constant values.
這裏寫圖片描述

屬性的值有三種:

    beforeDescendants:viewgroup會優先其子類控件而獲取到焦點

    afterDescendants:viewgroup只有當其子類控件不需要獲取焦點時才獲取焦點

    blocksDescendants:viewgroup會覆蓋子類控件而直接獲得焦點

如果遇到Listview和Button點擊事件衝突就在item佈局的根佈局添加次屬性(android:descendantFocusability),一般選afterDescendants把佈局中按鈕添加屬性android:focusable=”false”,最後在相應地方添加按鈕的點擊監聽事件就可以避免衝重點內容突,實現item及內部按鈕都可點擊不衝突的效果。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章