【问题标题】:Android MotionLayout Transition Listener not called未调用 Android MotionLayout 过渡侦听器
【发布时间】:2019-09-01 23:55:10
【问题描述】:

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

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

E/MotionLayout: WARNING NO 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")
            }
        })
    }
}

片段.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,动画是否播放/工作?我们可以看到您设置或膨胀视图的代码吗
  • 是的动画作品。我可以滑动 btn_slide。路径也正确显示。我编辑了我的问题以显示我的自定义视图的完整代码。
  • 尝试将motion_layout.setTransitionListener()替换为this.setTransitionListener(),同样,你可能有来自Activity/Fragment的引用,尝试从那里设置TransitionListener
  • 我刚刚尝试了你的两个建议,但不幸的是 LogCat 中出现了同样的错误,并且没有调用监听器
  • 将 MotionLayout 直接放入我的 Fragment 不会显示错误消息,但是仍然没有调用侦听器...

标签: android android-layout kotlin android-transitions android-motionlayout


【解决方案1】:

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

【讨论】:

    【解决方案2】:
    var motionLayout:MotionLayout?=null
     init {
           motionLayout =  LayoutInflater.from(context).inflate(R.layout.view_slider, this)
        }
    

    上面的行应该等同于一个命名视图..eg 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")
                }
            })
        }
    

    【讨论】:

      猜你喜欢
      • 2012-04-30
      • 1970-01-01
      • 2019-07-25
      • 2019-01-26
      • 1970-01-01
      • 1970-01-01
      • 2012-04-08
      • 2017-09-24
      相关资源
      最近更新 更多