我正在更新一个旧的 Android 项目,现在我从 RecyclerView 中反复收到这条日志语句:
W/RecyclerView:RecyclerView 不支持滚动到绝对位置。改为使用 scrollToPosition
当recyclerview第一次被填充显示或者recyclerview中的item被刷新时发生。
日志:
11-05 14:02:23.290 20209-20209/com.mydomain W/RecyclerView: RecyclerView does not support scrolling to an absolute position. Use scrollToPosition instead
11-05 14:02:23.290 20209-20209/com.mydomain W/RecyclerView: RecyclerView does not support scrolling to an absolute position. Use scrollToPosition instead
11-05 14:02:23.607 20209-20209/com.mydomain W/RecyclerView: RecyclerView does not support scrolling to an absolute position. Use scrollToPosition instead
11-05 14:02:23.607 20209-20209/com.mydomain W/RecyclerView: RecyclerView does not support scrolling to an absolute position. Use scrollToPosition instead
11-05 14:02:23.629 20209-20209/com.mydomain W/RecyclerView: RecyclerView does not support scrolling to an absolute position. Use scrollToPosition instead
[...]
该行重复了大约 40+ 次
但是,据我所知,在我的代码中我没有滚动到绝对位置。 (除非它可能是其他东西的副产品)
我找不到关于此警告的太多信息。
谁能提供见解?
最佳答案
我看到了完全相同的事情,在我的例子中,它是由 XML 属性引起的
android:animateLayoutChanges="true"
在 RecyclerView 的父级上 - 或者以编程方式设置 LayoutTransition在父 View 上,它做同样的事情。参见 this answer on a different SO question . (看起来它曾经是一个异常而不是旧 Android 版本的日志,我们在这方面很幸运。)
显然,删除属性可以修复它。如果(像我一样)你想保留它,请尝试以 RecyclerView 的直接父级没有它的方式组织你的 View ,例如 Sniper在我链接到的帖子中建议。在这里复制他的例子以防它被删除:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/top_parent_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:id="@+id/header_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:clickable="true"
android:animateLayoutChanges="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/header_arrow_icon"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="end|center_vertical"/>
</FrameLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
如果(像我一样)你需要 RecyclerView 上的 LayoutTransition 动画,在 RecyclerView 和带有布局动画的 View 之间放置一个包装 View 就足以消除警告:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:animateLayoutChanges="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>
</LinearLayout>
基本上确保 android:animateLayoutChanges 属性不在 RecyclerView 的父级上。
不过,即使有警告,实际的布局更改动画也能正常工作。我不知道向 View 层次结构中添加一个图层对性能的影响是否值得。更好的解决方案可能是将 RecyclerView 子类化并覆盖 scrollTo (x, y) 方法以不打印日志,例如 another answer on the same SO question .
关于android - RecyclerView 日志语句 : W/RecyclerView: RecyclerView does not support scrolling to an absolute position. 改为使用 scrollToPosition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47125982/