mohammad
mohammad

Reputation: 197

how to set listener for motion layout ending animation

I'm using motion layout in my project, when the user scrolls up, I hide my tabLayout and when the user scroll down, I show the tabLayout. now, I want to set a vibration as soon as tabLayout become appears. is there any way to set listener for ending of motion layout?

here is my motion description:

<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>

and here is my layout codes:

<?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>

Upvotes: 2

Views: 3176

Answers (1)

D Lad
D Lad

Reputation: 146

Create and add a MotionTransitionListener to your MotionLayout. Implement the onTransitionCompleted method and add your vibrate call in there.

Like this:

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)

Upvotes: 4

Related Questions