我看到了完全相同的事情,就我而言,这是由 XML 属性引起的
android:animateLayoutChanges="true"
在 RecyclerView 的父级上 - 或以编程方式设置布局转换 https://developer.android.com/reference/android/animation/LayoutTransition在父视图中,这做了同样的事情。看这个答案在另一个不同的问题上 https://stackoverflow.com/a/43027492/9434434。 (看起来它曾经是一个例外,而不是旧版 Android 版本中的日志,在这方面我们很幸运。)
显然,删除该属性可以修复它。如果(像我一样)您想保留它,请尝试以 RecyclerView 的直接父级没有它的方式组织您的视图,例如Sniper https://stackoverflow.com/users/4466472/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 和具有布局动画的视图之间放置一个包装视图就足以消除警告:
<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 的父级上。
不过,即使有警告,实际的布局更改动画也能正常工作。我不知道向视图层次结构添加一个层对性能的影响是否值得。更好的解决方案可能是子类化 RecyclerView 并重写 scrollTo (x, y) 方法以不打印日志,例如同一问题的另一个答案 https://stackoverflow.com/a/28432632/9434434.