记录dataBinding使用不规范导致点击事件不生效的问题

82 阅读1分钟

放一张代码图 image.png xml

<androidx.constraintlayout.widget.ConstraintLayout 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"> 
  
<Button  
android:id="@+id/b"  
android:layout_width="match_parent"  
android:layout_height="wrap_content"  
app:layout_constraintStart_toStartOf="parent"  
app:layout_constraintTop_toTopOf="parent" />  
</androidx.constraintlayout.widget.ConstraintLayout>

乍一看好像没什么问题,但是,怎么点击都没反应,下断点发现并没有回调onClick方法,找了很多原因,什么view遮挡什么乱七八糟的,但好像都不是,机智的群友,发现是binding的get方法的问题,然后发现是因为重写了get方法导致每次引用都创建新的binding,所以无法被回调onClick方法,解决方法是把get()去掉