我正在为我的页面使用 MotionLayout。我有两种状态,可通过视图的 OnSwipe 切换:
<Transition
motion:constraintSetStart="@+id/start"
motion:constraintSetEnd="@+id/end"
motion:duration="700"
motion:motionInterpolator="easeIn">
<OnSwipe
motion:dragDirection="dragUp"
motion:touchAnchorId="@+id/view"
motion:touchAnchorSide="bottom"
motion:touchRegionId="@+id/view" />
</Transition>
Run Code Online (Sandbox Code Playgroud)
我还想从代码中将 OnClickListener 添加到此视图,或在 scene.xml 文件中添加新的 Transition,这将执行与 OnSwipe Transition 不同的操作,并且将使用同一视图的 OnClick 触发。但它们都阻止了 OnSwipe。那么有没有办法让 OnSwipe 和 OnClick 在同一个视图上?
谢谢
我的主布局中有一个用于根容器的motionLayout。在里面,还有其他的景色。其中之一是 frameLayout,包含一个片段。该片段是一个页面,由一个 NestedScrollView 等组成... MotionLayout 具有仅用于水平滑动的 OnSwipe,而 NestedScrollView 应该只能垂直滚动。我不得不扩展 MotionLayout onInterceptTouchEvent 方法(查看下面的代码),并且只将那些非水平触摸事件传递给孩子们。到目前为止非常简单,并且有效。
问题是,当我开始在吸收某种触摸事件(如按钮或 NestedScrollView)的视图上滑动时,它会混淆 motionLayout 转换并立即跳过帧,因此锚点与我的鼠标位置相对应。过渡几乎完全在一个帧中进行,并扼杀了 UI 体验。我的猜测是,如果 motionLayout 将 X1 和 X2 用于转换,则 X1 值是导致问题的值,其值来自不应该出现的位置。但是当然,当我开始在触摸吸收元素上滑动时。
这是我的 customMotionLayout 的 OnInterceptTouchEvent 的覆盖方法:
override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
super.onInterceptTouchEvent(event)
return when (event.actionMasked) {
MotionEvent.ACTION_DOWN -> {
previousX = event.x.toInt()
previousY = event.y.toInt()
mIsScrolling = false
false
}
MotionEvent.ACTION_CANCEL, MotionEvent.ACTION_UP -> {
mIsScrolling = false
false
}
MotionEvent.ACTION_MOVE -> {
if (mIsScrolling) true
else {
var xMotion: Int …Run Code Online (Sandbox Code Playgroud) android android-touch-event android-constraintlayout android-motionlayout
我想在用户单击或滑动时播放动画。但是我们可以在 MotionLayout 中处理这两种行为吗?它们可以完美地分开工作,但如果我在同一场景中添加OnClick和OnSwipe ,则只有OnClick有效。有什么解决方法吗?
我有一个带有以下“layoutDescription”的motionlayout(场景文件)
<Transition
motion:constraintSetStart="@+id/start"
motion:constraintSetEnd="@+id/end"
motion:duration="1000">
<OnSwipe
motion:touchAnchorId="@+id/button"
motion:touchAnchorSide="left"
motion:dragDirection="dragLeft" />
</Transition>
<ConstraintSet android:id="@+id/start">
<!-- ConstraintSet for starting -->
</ConstraintSet>
<ConstraintSet android:id="@+id/end">
<!-- ConstraintSet for end -->
</ConstraintSet>
Run Code Online (Sandbox Code Playgroud)
我从这里期望的是,如果用户将“按钮”拖向左侧,那么它应该执行转换,但实际发生的是当用户从屏幕中的任何位置向左拖动(向左滑动)时,执行此转换!
如何仅在拖动指定视图时限制动画工作?
(我使用 "constraintlayout:2.0.0-beta2" )
我正在使用带有场景 xml 的 MotionLayout:
<Transition
motion:constraintSetStart="@+id/start"
motion:constraintSetEnd="@+id/end"
>
<OnSwipe
motion:touchAnchorId="@+id/v_top_sheet"
motion:touchRegionId="@+id/v_top_sheet_touch_region"
motion:touchAnchorSide="bottom"
motion:dragDirection="dragDown" />
</Transition>
Run Code Online (Sandbox Code Playgroud)
2ConstraintSets只引用了 2 个视图 ID:v_notifications_container和v_top_sheet。
在我的活动中,我想将一个普通的 ClickListener 设置为这个 MotionLayout 中的其他视图之一:
iv_notification_status.setOnClickListener { Timber.d("Hello") }
Run Code Online (Sandbox Code Playgroud)
执行此行,但从未触发 ClickListener。我搜索了其他帖子,但其中大多数都涉及在与motion:touchAnchorId. 这不是这里的情况。ClickListener 设置为在 MotionLayout 设置中未曾提及的视图。如果我删除该app:layoutDescription属性,则单击有效。
我也尝试使用setOnTouchListener,但它也从未被调用过。
如何在 MotionLayout 中设置单击侦听器?
android onclicklistener android-constraintlayout android-motionlayout
android ×5