未调用TransitionListener

时间:2019-04-11 11:00:43

标签: android kotlin android-motionlayout

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

幻灯片动画效果很好。但是我在LogCat中收到错误:

  

E / MotionLayout:警告否app:layoutDescription标记

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>

scene_slider.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回调?

2 个答案:

答案 0 :(得分:0)

MotionLayout在片段中无法正常工作。尝试升级到Beta-2版本。

答案 1 :(得分:0)

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

上面的行应该等同于一个命名视图..例如 val motionLayout。

这个命名变量应该用于 onFinish inflate..i.e

  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")
            }
        })
    }