回收器视图在第一次加载时自动滚动到内部回收器视图

回收器视图在第一次加载时自动滚动到内部回收器视图

本文介绍了回收器视图在第一次加载时自动滚动到内部回收器视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个根垂直回收器视图,项目是

I have a root vertical recycler view and items are

  1. ViewPager
  2. 水平回收器视图
  3. 水平回收器视图...

第一次加载数据时,当入口动画停止时,根回收器视图轻微滚动,第二项(回收器视图)完全可见.

When the data is first loaded, when entry animation stops, the root recycler view is slightly scrolled and 2nd item(the recycler view) is completely visible.

为什么会这样?以及如何避免这种自动滚动.

Why is this happening? And how to avoid this automatic scrolling.

推荐答案

当你调用 notifyDataSetChangednotifyItemsInserted 时,它会触发一个 requestLayoutRecyclerView.

When you call notifyDataSetChanged or notifyItemsInserted, it triggers a requestLayout in the RecyclerView.

布局后,RecyclerView 尝试recoverFocusFromState.当它找到第一个可聚焦的孩子时,它会尝试将其完全显示出来.如果该可聚焦子级在布局结束时部分可见,则会导致滚动并且可聚焦子级将完全可见.

After the layout, RecyclerView tries to recoverFocusFromState. And when it finds the first focusable child, it tries to bring it completely into view. If that focusable child is partially visible by the end of layout, it will cause a scroll and the focusable child will be fully visible.

在上面的例子中,子 RecyclerViewfocusable 子.ProgressBar 或任何其他 focusable 视图

In the above case, the child RecyclerView is the focusable child. The same can happen with ProgressBar or any other focusable view

如果您知道您的子视图不可聚焦.您可以添加

If you know your child views are not focusable. You can add

    android:descendantFocusability="blocksDescendants"

到你的根 RecyclerView

focusable="false" 添加到不可聚焦的孩子

Add focusable="false" to your non focusable childs

这篇关于回收器视图在第一次加载时自动滚动到内部回收器视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 09:52