关于NestedScrollView嵌套RecyclerView页面载入时总是滑动到最底部

看标题是不是不太明白,来两张图片:
      

正常情况下,我们期待的是进入页面以后显示左图效果,可是由于NestedScrollView嵌套RecyclerView,导致Recyclerview占据焦点导致整个NestedScrollView内容上滑。知道原因以后解决这个bug就容易多了。

方法一:在根布局设置android:descendantFocusability=”blocksDescendants” ;

android:descendantFocusability 有三种值:

beforeDescendants:viewgroup会优先其子类控件而获取到焦点
afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点
blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点

这种方法,会造成页面中Editext焦点被抢导致无法输入,需要用到第二种方法。

方法二:对于有Editext的页面需要在根布局使用 :

android:focusable="true"
android:focusableInTouchMode="true"

方法三:在NestedScrollView顶部第一个控件使用 :

android:focusable="true"
android:focusableInTouchMode="true" 

这种方法我觉得有点不太可靠,因为有时候是可行的,有时候是不行的。。。。

猜你喜欢

转载自blog.csdn.net/u012230055/article/details/80016802
今日推荐