正常情况下,由于NestedScrollView/ScrollView 嵌套RecyclerView或者gridview,可能会导致Recyclerview或者gridview占据焦点导致整个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"
本文介绍了解决NestedScrollView或ScrollView嵌套RecyclerView时,因焦点问题导致内容上滑的解决方案。通过调整descendantFocusability属性并设置focusable属性,确保页面中EditText正常获取焦点。

被折叠的 条评论
为什么被折叠?



