android - 如何为运动布局结束动画设置监听器
问题描述
我在我的项目中使用运动布局,当用户向上滚动时,我隐藏我的 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?, 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)
推荐阅读
- r - 拟合混合模型时出错:观察数 <= 随机效应数
- typescript - Visual Studio 代码是否有一个键绑定来注释打字稿中的类型
- javascript - Vue.js 我的网络应用程序无法连接到 firebase 数据库
- c++ - 如何使输出语句“虚拟”?
- runtime-error - 无法在 Dyalog APL 中使用用户命令
- javascript - 在 React 路由器中使用“链接到”时如何刷新页面?
- java - 用 Read 和 Write 方法实现类
- python - 将迭代函数转换为递归函数
- rust - 使用特征边界指定返回类型与 impl 之间的区别
- visual-studio-code - 如何在 VSCode 中获取相对路径?