如何在XML中设置OnClick函数包括使用ViewBinding MVVM的布局

发布于 2025-02-12 09:36:27 字数 2309 浏览 1 评论 0原文

如何在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南薇 2025-02-19 09:36:27

您的点击功能无法正常工作,因为viewModel toolbar_layout.xml(布局y)中的变量(布局y)将其包含在active> activity_mail.mail.xml (布局x)。

您可以设置这样的变量:

<include
            android:id="@+id/toolbar"
            layout="@layout/toolbar_layout"
            bind:viewModel="@{viewModel}"
            ... />

链接到文档。

更新:绑定变量

Your click-function is not working, because the viewModel variable in toolbar_layout.xml (Layout Y) is not set when including it into activity_mail.xml (Layout X).

You can set the variable like this:

<include
            android:id="@+id/toolbar"
            layout="@layout/toolbar_layout"
            bind:viewModel="@{viewModel}"
            ... />

Link to documentation.

update: binding the variable

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文