NestedScrollView、ScrollView 加载完自动滑动至底部问题的解决方案
正常情况下,由于 NestedScrollView / ScrollView 嵌套 RecyclerView ,可能会导致 Recyclerview 占据焦点导致整个 NestedScrollView / ScrollView 内容上滑。
此问题的解决方案如下:
1. 在根布局设置android:descendantFocusability=”blocksDescendants” ;
android:descendantFocusability 有三种值:
beforeDescendants:viewgroup会优先其子类控件而获取到焦点
afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点
blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点
这种方法,会造成页面中Editext焦点被抢导致无法输入,需要用到第二种方法。
2, 对于有Editext的页面需要在根布局使用 :
android:focusable="true"
android:focusableInTouchMode="true"
3. 在NestedScrollView/ScrollView顶部第一个控件使用 :
android:focusable="true"
android:focusableInTouchMode="true"