赞
踩
当ScrollView里面包含Recycleview或Listview,并且数据一屏显示不完时,打开页面,会自动滚动到页面底部,也就是Recycleview的最后一项。
给scrollview的直接子view添加下面属性就可以解决。(scrollview只能有一个直接子view;给scrollview加是不行的,不知道为啥)
解决办法一:(推荐,最简单)
android:descendantFocusability="blocksDescendants"
解决办法二:
- android:focusable="true"
- android:focusableInTouchMode="true"
出现原因:Scrollview的childview,拥有获得焦点的能力,高度很大的childview获取到焦点,导致Scrollview滑到它的底部。(这又是为什么呢?等看看Scrollview源码再分析吧)
1. android:descendantFocusability
这个属性专门解决子父控件焦点的冲突,或者说,是处理viewgroup和其子控件获取焦点的顺序。
API 描述 :
Defines the relationship between the ViewGroup and its descendants when looking for a View to take focus.
Must be one of the following constant values.
属性的值有三种:
beforeDescendants:viewgroup会优先其子类控件而获取到焦点
afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点
blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点
参考文章 https://www.cnblogs.com/wangmars/p/3488959.html
2.focusable & focusableInTouchMode
通过将这两个属性设置为true可以强制让Scrollview获得焦点,从而不让Scrollview的childview自动获取焦点
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。