如何在XML中设置OnClick函数包括使用ViewBinding MVVM的布局
如何在MVVM体系结构中包含布局中实现OnClick事件,
我们在布局x中包含一个布局y
示例布局x(activity_main.xml)
<?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">
<data>
<import type="android.view.View" />
<variable
name="viewModel"
type=".view.ViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
示例布局y(toolbar_layout.xml)
<?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">
<data>
<import type="android.view.View" />
<variable
name="viewModel"
type=".view.ViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_height="match_parent">
<ImageView
android:id="@+id/img_back"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:background="@drawable/circle_white"
android:onClick="@{(v) -> viewModel.clickEvent()}"
android:padding="12dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_baseline_arrow_back_24" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
在上述方案中,点击函数不起作用,如何在不依赖数据框的情况下使用ViewBinding实现此目标?
How to achieve onclick event in include layout in MVVM architecture
we have a layout Y includes in layout X
sample layout X (activity_main.xml)
<?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">
<data>
<import type="android.view.View" />
<variable
name="viewModel"
type=".view.ViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
sample layout Y (toolbar_layout.xml)
<?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">
<data>
<import type="android.view.View" />
<variable
name="viewModel"
type=".view.ViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_height="match_parent">
<ImageView
android:id="@+id/img_back"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:background="@drawable/circle_white"
android:onClick="@{(v) -> viewModel.clickEvent()}"
android:padding="12dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_baseline_arrow_back_24" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
in the above scenario, the click function is not working, how to achieve this with viewBinding without depending on dataBinding?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的点击功能无法正常工作,因为
viewModel
toolbar_layout.xml
(布局y)中的变量(布局y)将其包含在active> activity_mail.mail.xml (布局x)。
您可以设置这样的变量:
链接到文档。
更新:绑定变量
Your click-function is not working, because the
viewModel
variable intoolbar_layout.xml
(Layout Y) is not set when including it intoactivity_mail.xml
(Layout X).You can set the variable like this:
Link to documentation.
update: binding the variable