android - 使用数据绑定(bind)在xml中的工具栏导航图标上应用点击事件

标签 android xml data-binding

我是数据绑定(bind)方面的新手,是否知道如何使用数据绑定(bind)在 xml 中直接在工具栏导航图标上应用点击事件。

我的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"
    xmlns:bind="http://schemas.android.com/apk/res-aut">
    <data class="SelectCuisineBinding">
        <variable
            name="viewModel"
            type="com.aman.camellia.kniterider.viewmodel.SelectCuisineViewModel"/>
        <variable
            name="activity"
            type="com.aman.camellia.kniterider.view.activity.SelectCuisineActivity"/>
    </data>

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.aman.camellia.kniterider.view.activity.SelectCuisineActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            app:navigationIcon="?homeAsUpIndicator"

            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:title="@string/Cuisines"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_select_cuisine"
        app:viewModel="@{viewModel}"

        app:activity="@{activity}"/>



</android.support.design.widget.CoordinatorLayout>
</layout>

如何处理 viewholder 中的点击事件。 有什么想法吗...谢谢

最佳答案

Android默认提供绑定(bind)适配器或支持添加点击监听器。我们只需要修改属性app:navigationOnClickListener。命名空间是 xmlns:app="http://schemas.android.com/apk/res-auto"

<androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/blue_grey_600"
            app:navigationIcon="@drawable/ic_close_icon_resized"
            app:title="@{viewModel.toolBarText}"
            app:titleTextAppearance="@style/EmiToolbar.Text"
            app:navigationOnClickListener="@{() -> viewModel.onToolbarNavigationClick()}"
            app:contentInsetStart="0dp"
            app:contentInsetStartWithNavigation="0dp"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

关于android - 使用数据绑定(bind)在xml中的工具栏导航图标上应用点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48719748/

相关文章:

android - 如何关闭操作点击通知?

java - 删除菜单 Java Android

xml - Powershell:按元素索引将XML节点值选择为变量

javascript - Angular2 中优化的数据绑定(bind)机制如何工作

wpf - 在 UserControl 中设置 DataContext 会影响父级中的绑定(bind)

java - 使用 JNI 将 float* 转换为 jfloatArray

android - 如何在 Parcelable 类中写入和读取 org.joda.time.Date

xml - 我不想解析 XML 中的某些标签

c++ - Xerces-C 问题;调用对象析构函数时出现段错误

wpf - 在编译时验证 XAML 中的数据绑定(bind)