最近遇到的問題是,Fragment 中為一個 NextedScrollView ,裡面從上至下有 Button 等不同的 View,最下方是 RecyclerView,但是在 Fragment 顯示時,NextedScrollView 會自動滑動到 RecyclerView 的位置。
會這樣的原因是因為,NextedScrollView 在繪製時,會去找尋 child view 中有沒有 has focus 的 child view,如果有,則滾動到它的位置。而 RecyclerView 是它找到的 has focus 的 child view,因此位置就移動到 RecyclerView 了。
這也是為什麼有人呼叫 RecyclerView.setFoucsable(false) 後,就不會發生自動滾動了,因為沒有找到 has focus 的 child view。不過最好的解法,應該是在 NextedScrollView 的直接子 view 中增加 descendantFocusability 屬性並將之設為 blocksDescendants, 因為 NextedScrollView 繪製過程中看到了 child view 有這個屬性,就不會再做自動滾動的動作了。
- <LinearLayout
- android:id="@+id/container"
- android:descendantFocusability="blocksDescendants"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
Reference:
沒有留言:
張貼留言