如何设置动作布局结束动画的侦听器

问题描述

我在项目中使用运动布局,当用户向上滚动时,我隐藏了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>

解决方法

创建一个 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?,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)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...