ScrollView內部巢狀Recyclerview,防止recyclerview自動獲得焦點
阿新 • • 發佈:2019-01-06
在寫一個ScrollView 巢狀Viewpager和RecyclerView的佈局時,RecyclerView老是自動獲得焦點然後在ScrollView
內部上下自動滾動,嘗試了在ScrollView的直接子LinearLayout使用
android:focusable="true" android:focusableInTouchMode="true"
但在一些情況下 ScrollView還是會自動獲得焦點然後滾動起來,最後的方案
解決方案:
在ScrollView下面的LinearLayout加上android:descendantFocusability="blocksDescendants"屬性, 截獲ListView的焦 點, 從而達到禁止ScrollView自動滾動. 順便解釋下這個屬性的其他用法:
該屬性是當一個為view獲取焦點時,定義viewGroup和子控制元件兩者之間的關係。屬性的值有三種:
beforeDescendants:viewgroup會優先其子類控制元件而獲取到焦點
afterDescendants:viewgroup只有當其子類控制元件不需要獲取焦點時才獲取焦點
blocksDescendants:viewgroup會覆蓋子類控制元件而直接獲得焦點