Android数据绑定条件菜单?

3

我正在尝试使用数据绑定库基于用户角色设置不同的菜单。

在下面的代码片段中,如果用户是教育工作者,那么我会设置activity_main_educator_drawer,否则设置activity_main_child_drawer

<?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-auto">
    <data>
        <variable
            name="user"
            type="sc.me.twelve.viewmodels.UserViewModel" />
    </data>
    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">

        <include
            layout="@layout/app_bar_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header_main"
            app:menu="@{user.getModel.getRole.equals(`educator`) ? @menu/activity_main_educator_drawer : @menu/activity_main_child_drawer}" >

            <include
                layout="@layout/nav_header_main"
                bind:user="@{user}" />

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

    </android.support.v4.widget.DrawerLayout>
</layout>

看起来不错,但是我遇到了这个错误:

****/ data binding error ****msg:Identifiers must have user defined types from the XML file. activity_main_educator_drawer is missing it

最后

George Mount的回答是正确的,我只是添加了BindingAdapter

@BindingAdapter("app:menu")
public static void setMenu(NavigationView navigationView, int id) {
    navigationView.inflateMenu(id);
}

我认为您没有使用<layout>标签包装您的NavigationView。 - D.J
1
我可能错了,但我认为您需要交换"'- app:menu ='@ {viewModel.getModel.getRole.equals("educator")?@ menu / activity_main_educator_drawer:@ menu / activity_main_child_drawer}' - yennsarah
据我所知 - 现在无法绑定“@menu”。 - Ekalips
@Amylinn 我尝试了所有的", '和```组合,但都不起作用。 - Samuele Colombo
@SamueleColombo 我之前也遇到了同样的问题,只找到了一种变通方法,但没有直接的解决方案。 - Ekalips
显示剩余3条评论
1个回答

3

看起来菜单资源没有被识别。请尝试使用以下表达式:

    <android.support.design.widget.NavigationView
        ...
        app:menu="@{user.getModel.getRole.equals(`educator`) ? R.menu.activity_main_educator_drawer : R.menu.activity_main_child_drawer}" >

请确保导入R类:

<data>
    <import type="sc.me.twelve.R"/>
</data>

无论您的软件包是什么。

我看到您表达中有一些小奇怪之处。我认为可以简化:

app:menu="@{user.model.role.equals(`educator`) ? R.menu.activity_main_educator_drawer : R.menu.activity_main_child_drawer}"

假定访问器方法采用 "get" 或 "is" 前缀。

我尝试了你的解决方案,但它没有起作用,并显示以下错误:Cannot find the setter for attribute 'app:menu' with parameter type int on android.support.design.widget.NavigationView。这很明显,我认为没有变通的方法。 - Samuele Colombo
我假设你已经写了一个绑定适配器。如果没有,你应该写一个接受 int 类型的适配器。 - George Mount

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接