美文网首页
Android RecyclerView 莫名自动滚动问题

Android RecyclerView 莫名自动滚动问题

作者: 哈哈V青春 | 来源:发表于2019-08-10 15:19 被阅读0次

在项目开发过程遇到一个问题,切换布局中某个子view的visibility为gone后,RecyclerView 自己莫名其妙的滚动了一点。通过网上查找发现,这是RecyclerView 抢占焦点导致。可以用以下方式解决:
父布局设置属性descendantFocusability 为 blocksDescendants,RecyclerView 设置 属性overScrollMode 为never。

<com.xxxxxxxxxxxx.Layout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants">

<com.xxxxxxxxxx.view.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:overScrollMode="never" />

</com.xxxxxxxxxxxx.Layout>

descendantFocusability 属性有三种属性值:
1、beforeDescendants:viewgroup会优先其子类控件而获取到焦点
2、afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点
3、blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点

相关文章

网友评论

      本文标题:Android RecyclerView 莫名自动滚动问题

      本文链接:https://www.haomeiwen.com/subject/wvsrjctx.html