首页 > 解决方案 > 使用 Epoxy / DataBinding 设置为可见时,Android Lottie 动画不会自动启动

问题描述

我在带有 DataBinding 的 Android 项目中使用 Lottie 和 Epoxy。当 RecyclerView 被捕捉时,我想用 LottieImageView 开始动画,但是 LottieImageView 在设置为autoPlay=true.

环氧树脂控制器如下:

class MainController : EpoxyController() {

    var currentSnappedPosition = 0
        set(value) {
            field = value
            requestModelBuild()
        }

    override fun buildModels() {
        repeat(10) {
            ItemBindingModel_()
                .id(modelCountBuiltSoFar)
                .isSnapped(currentSnappedPosition == modelCountBuiltSoFar)
                .addTo(this)
        }
    }
}

currentSnappedPosition当 RecyclerView 项目被捕捉时更新,然后rquestModelBuild()被调用。

布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

        <import
            type="android.view.View" />

        <variable
            name="isSnapped"
            type="boolean" />

    </data>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.airbnb.lottie.LottieAnimationView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="@{isSnapped ? View.VISIBLE : View.GONE }"
            app:lottie_autoPlay="true"
            app:lottie_loop="true"
            app:lottie_rawRes="@raw/lunar_new_year"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:text="not snapped..."
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="@{isSnapped ? View.GONE : View.VISIBLE }"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </FrameLayout>

</layout>

LottieAnimationView 设置为在项目被捕捉时可见。

整个项目在 GitHub 上:

有谁知道当 LottieAnimationView 设置可见时如何自动启动?

标签: androidlottieepoxy

解决方案


推荐阅读