ScrollView,SwipeRefreshLayout嵌套RecyclerView时,RecyclerView总会把第一个Item顶出页面的解决办法...

ScrollView,SwipeRefreshLayout嵌套RecyclerView时,当离开页面又回来,或者做RecyclerView展开收缩总会把第一个item顶出头部(部分or整个),原因是RecyclerView抢了焦点。知道原因就好办了!多方查找,给出两个解决方案。

方案一:把ScrollView,SwipeRefreshLayout最上边的那个控件加上几句代码。

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

方案二:timshinlee提供了一个更简单的方法

ScrollView、SwipeRefreshLayout下只能包裹一个控件,所以我们常包裹一个LinearLayout(或RelativeLayout),然后再包裹我们的其他控件(比如RecyclerView等),那一行代码就写在LinearLayout(或RelativeLayout)上。

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:descendantFocusability="blocksDescendants"
            android:orientation="vertical">

推荐使用方案二,简单明了!

关于android:descendantFocusabilityAPI描述如下:

 
android:descendantFocusability

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://my.oschina.net/hkceey/blog/860756

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值