RecyclerView Animate布局更改无法正常工作

时间:2016-03-30 13:03:41

标签: android animation android-recyclerview

我有下一个结构:

活动1:

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:animateLayoutChanges="true"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/FAB_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</android.support.design.widget.CoordinatorLayout>

活动2:

Form to add new item to the recycler View.

RecyclerView的XML:

<android.support.v7.widget.RecyclerView
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/recycler_view"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:animateLayoutChanges="true"/>

我在第二个Activity中创建了该项,并使用startActivityForResult在activity one中添加该项,然后调用adapter.notifyItemInserted(0)。

问题是当添加项目时,插入动画无法正常工作,但如果我不使用第二个活动并在第一个活动中创建项目并添加到适配器,它可以正常工作。

¿我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您的xml结构可能存在问题,请检查layout_behavior并使用CoordinatorLayout Android CoordinatorLayout

有关如何使用CoordinatorLayoutlayout_behavior检查this blog

的详细指南

我还发现android:animateLayoutChanges="true"在可滚动的RecyclerView中使用非常小故障/错误,我建议不要使用它。对于不可滚动的RecyclerView,它可以正常工作。