【问题标题】:how to set listener for motion layout ending animation如何为运动布局结束动画设置监听器
【发布时间】:2021-03-03 19:38:29
【问题描述】:

我在我的项目中使用运动布局,当用户向上滚动时,我会隐藏我的 tabLayout,当用户向下滚动时,我会显示 tabLayout。 现在,我想在 tabLayout 出现时立即设置振动。 有什么方法可以设置监听器以结束运动布局?

这是我的动作描述:

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


    <Transition
        android:id="@+id/my_transition"
        app:constraintSetEnd="@+id/ending_set"
        app:constraintSetStart="@+id/starting_set"
        app:duration="5000">

        <OnSwipe
            app:dragDirection="dragUp"
            app:touchAnchorId="@+id/viewPager"
            app:touchAnchorSide="top" />


    </Transition>


    <ConstraintSet android:id="@+id/starting_set">

        <Constraint
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/tabLayout" />

        <Constraint
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:alpha="1"
            app:layout_constraintTop_toTopOf="parent" />


    </ConstraintSet>


    <ConstraintSet android:id="@+id/ending_set">

        <Constraint
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


        <Constraint
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:alpha="0"
            app:layout_constraintTop_toTopOf="parent" />

    </ConstraintSet>


</MotionScene>

这是我的布局代码:

<?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"
    xmlns:tools="http://schemas.android.com/tools">

    <androidx.constraintlayout.motion.widget.MotionLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layoutDescription="@xml/news_motion"
        tools:context=".main.news.AnalysisCategoryFragment">




        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#E6E6E6"
            android:elevation="5dp"
            app:layout_constraintTop_toTopOf="parent"
            app:tabSelectedTextColor="@color/colorPrimaryDark"
            app:tabTextColor="@color/gray" />

        <androidx.viewpager2.widget.ViewPager2
            android:elevation="5dp"
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/tabLayout" />

    </androidx.constraintlayout.motion.widget.MotionLayout>
</layout>

【问题讨论】:

    标签: android kotlin android-animation android-motionlayout


    【解决方案1】:

    创建MotionTransitionListener 并将其添加到您的MotionLayout。实现onTransitionCompleted 方法并在其中添加您的振动呼叫。

    像这样:

    val transitionListener = object : MotionLayout.TransitionListener {
    
            override fun onTransitionStarted(p0: MotionLayout?, startId: Int, endId: Int) {
              //nothing to do
            }
    
            override fun onTransitionChange(p0: MotionLayout?, startId: Int, endId: Int, progress: Float) {
                //nothing to do
            }
    
            override fun onTransitionCompleted(p0: MotionLayout?, currentId: Int) {
                viewToVibrate.vibrate()
            }
            override fun onTransitionTrigger(p0: MotionLayout?, triggerId: Int, positive: Boolean, progress: Float) {
                //not used here
            }
    
         motionLayout.setTransitionListener(transitionListener)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-14
      • 1970-01-01
      • 1970-01-01
      • 2012-10-29
      • 1970-01-01
      相关资源
      最近更新 更多