将单击侦听器添加到任何视图后,运动布局将停止工作



所以情况是这样的。我有多个OnSwipe和OnClick的运动布局设置,它们协同工作并创建流畅的动画。但是,当我将单击侦听器添加到运动布局的任何子级时,就像这个简单的一样:

darkenForeground.setOnClickListener {
Log.e("HI","");
}

所有 Motion 布局功能都停止工作,并且不会响应任何滑动或单击。甚至没有调用新添加的点击侦听器。如果您有任何想法,为什么会发生这种情况,请告诉我。欢迎任何帮助。谢谢!

这是完整的布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:id="@+id/mainMotion"
app:layoutDescription="@xml/activity_main_scene">

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/bottomContainer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="234dp"
android:background="@color/colorPrimaryDark"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HELLO"
android:textSize="30dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:textColor="@android:color/white"/>

</androidx.constraintlayout.widget.ConstraintLayout>
<FrameLayout
android:id="@+id/darkenForeground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0.0"
android:background="@android:color/black"/>
<com.lijiankun24.shadowlayout.ShadowLayout
android:id="@+id/topContainer"
android:layout_width="match_parent"
android:layout_height="250dp"
app:layout_constraintTop_toTopOf="parent"
app:shadowColor="#66000000"
app:shadowDy="2dp"
app:shadowRadius="4dp"
app:shadowShape="rectangle"
app:shadowSide="bottom">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent">
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/top_gradient"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

</com.lijiankun24.shadowlayout.ShadowLayout>
<ImageView
android:id="@+id/draggable"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:alpha="0.5"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:src="@drawable/drag_shape"
app:layout_constraintBottom_toBottomOf="@id/topContainer"
android:layout_marginBottom="15dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />


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

全场景

<?xml version="1.0" encoding="utf-8"?>
<MotionScene 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:motion="http://schemas.android.com/apk/res-auto">
<Transition
motion:constraintSetEnd="@+id/end"
motion:constraintSetStart="@id/start"
motion:duration="1000">
<KeyFrameSet>
</KeyFrameSet>
</Transition>
<ConstraintSet android:id="@+id/start">
<Constraint
android:id="@+id/topContainer"
android:layout_width="match_parent"
android:layout_height="250dp"
motion:layout_constraintTop_toTopOf="parent" />
</ConstraintSet>
<ConstraintSet android:id="@+id/end">
</ConstraintSet>
<ConstraintSet android:id="@+id/expanded_top" >
<Constraint
android:id="@+id/topContainer"
android:layout_width="match_parent"
android:layout_height="450dp"
motion:layout_constraintTop_toTopOf="parent" />
<Constraint
android:id="@+id/darkenForeground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0.5" />
</ConstraintSet>
<Transition
motion:constraintSetStart="@+id/start"
motion:constraintSetEnd="@+id/expanded_top"
motion:duration="300">
<OnSwipe motion:dragDirection="dragDown"
motion:touchRegionId="@id/topContainer"
motion:touchAnchorId="@id/draggable" />
<OnClick />
</Transition>
</MotionScene>

完整活动

package com.gooofystudios.calc
import android.os.Bundle
import android.util.DisplayMetrics
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import java.security.AccessController.getContext

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initViews()
}
private fun initViews(){
/*darkenForeground.setOnClickListener {
Log.e("HI","");
}*/
///Uncommenting this will cause motion layout to stop working
}

///Ignore this (is not even called)
fun Int.toPx(): Int{
val displayMetrics: DisplayMetrics = resources.displayMetrics
return Math.round(this * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT))
}
}

从 XML 添加点击监听器 ..即

<FrameLayout
android:id="@+id/darkenForeground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0.0"
android:onClick="doSomething"
android:background="@android:color/black"/>

最新更新