Android MotionLayout过渡监听器未被调用

11

我正在尝试制作一个幻灯片动画。为此,我使用了一个包含两个ConstraintSet的MotionScene MotionLayout。

幻灯片动画可以正常工作。然而,在LogCat中我得到了一个错误:

E/MotionLayout: WARNING NO app:layoutDescription tag

view_slider.xml

<androidx.constraintlayout.motion.widget.MotionLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/motion_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
app:layoutDescription="@xml/scene_slider"
app:showPaths="true">

<View
    android:id="@+id/btn_slide"
    android:layout_width="64dp"
    android:layout_height="64dp"
    android:background="@color/grey26" />
</androidx.constraintlayout.motion.widget.MotionLayout>

场景滑块器.xml:

<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:motion="http://schemas.android.com/apk/res-auto">

    <Transition
        motion:constraintSetStart="@+id/start"
        motion:constraintSetEnd="@+id/end"
        motion:duration="1000">

        <OnSwipe
            motion:touchAnchorId="@+id/btn_slide"
            motion:touchAnchorSide="right"
            motion:dragDirection="dragRight"/>

    </Transition>

    <ConstraintSet android:id="@+id/start">
        <Constraint
            android:id="@+id/btn_slide"
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:layout_marginStart="8dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent" />
    </ConstraintSet>

    <ConstraintSet android:id="@+id/end">
        <Constraint
            android:id="@+id/btn_slide"
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:layout_marginEnd="8dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintTop_toTopOf="parent" />
    </ConstraintSet>

</MotionScene>
SliderView.kt
class SliderView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : MotionLayout(context, attrs, defStyleAttr) {

    init {
        LayoutInflater.from(context).inflate(R.layout.view_slider, this)
    }

    override fun onFinishInflate() {
        super.onFinishInflate()

        motion_layout.setTransitionListener(object : MotionLayout.TransitionListener {
            override fun onTransitionCompleted(motionLayout: MotionLayout?, currentId: Int) {
                debug(javaClass.simpleName, "transition completed")
            }

            override fun onTransitionChange(motionLayout: MotionLayout?, startId: Int, endId: Int, progress: Float) {
                debug(javaClass.simpleName, "progress = $progress")
            }
        })
    }
}

片段文件(fragment.xml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/red">

    <com.myapp.utils.views.SliderView
        android:id="@+id/slider"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

我可能错过了一些东西,因为代码中的MotionLayout有一个layoutDescription。

如何修复此错误以及为什么现在会调用TransitionListener回调函数?


我也遇到了一个问题,当我从代码中更改场景时,TransitionListener没有被调用,动画是否播放/工作?我们能看到您设置或填充视图的代码吗? - User One
是的,动画起作用了。我可以滑动btn_slide。路径也正确显示了。我编辑了我的问题以显示自定义视图的完整代码。 - Azartys
尝试将motion_layout.setTransitionListener()替换为this.setTransitionListener(),同时,您可能有一个来自Activity/Fragment的引用,请尝试从那里设置TransitionListener。 - User One
我刚刚尝试了你的两个建议,但不幸的是LogCat中仍然出现相同的错误,并且监听器没有被调用。 - Azartys
让我们在聊天中继续这个讨论 - Azartys
显示剩余3条评论
2个回答

1
var motionLayout:MotionLayout?=null
 init {
       motionLayout =  LayoutInflater.from(context).inflate(R.layout.view_slider, this)
    }

上面的代码应该与一个命名视图相等,例如val motionLayout。

这个命名变量应该在 onFinish inflate 中使用,即:

  override fun onFinishInflate() {
        super.onFinishInflate()

        motionLayout?.setTransitionListener(object : MotionLayout.TransitionListener {
            override fun onTransitionCompleted(motionLayout: MotionLayout?, currentId: Int) {
                debug(javaClass.simpleName, "transition completed")
            }

            override fun onTransitionChange(motionLayout: MotionLayout?, startId: Int, endId: Int, progress: Float) {
                debug(javaClass.simpleName, "progress = $progress")
            }
        })
    }

0

MotionLayout 在片段中未正常工作。请尝试升级至 beta-2 版本。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接