當在Fragment使用NestedScrollView嵌套RecyclerView時,來回切換Fragment時會發現NestedScrollView會自動向上滾動一段距離;
我第一反應就是有佈局搶焦點了,有時候如果有EdutText也會有類似的現象,解決辦法就是在NestedScrollView裏的唯一佈局設置自動獲取焦點,代碼如下:
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EEEEEE">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"//關鍵
android:focusableInTouchMode="true"//關鍵
android:orientation="vertical">
</LinearLayout>
</androidx.core.widget.NestedScrollView>