如何通过 include 标签传递可观察字段?

How to pass observable fields through include tag?

我有工具栏微调器的布局:

<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="selectedItem"
            type="int" />
    </data>

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:popupTheme="@style/Theme.Paper.Toolbar.Popup"
        app:theme="@style/Theme.Paper.Toolbar">

        <Spinner
            android:id="@+id/toolbar_spinner"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            app:theme="@style/Theme.Paper.Toolbar.Spinner"
            app:popupTheme="@style/Theme.Paper.Toolbar.Spinner.Popup"
            app:selectedItem="@{selectedItem}"/>

    </androidx.appcompat.widget.Toolbar>
</layout>

我将该布局包含在片段布局中,如下所示:

<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>

        <variable
            name="model"
            type="com.contedevel.timetable.models.WeekViewModel" />
    </data>

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.contedevel.timetable.activities.MainActivity">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <include
                android:id="@+id/toolbar_layout"
                layout="@layout/toolbar_spinner"
                app:selectedItem="@{model.selectedWeekPosition}"/>

        </com.google.android.material.appbar.AppBarLayout>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>

这是我的ViewModel

class WeekViewModel(application: Application) : AndroidViewModel(application) {
    val selectedWeekPosition = ObservableInt(0)
}

但是 selectedItem 仅在 activity 开始时更改一次。当我尝试在 onOptionsItemSelected 中更改它时,没有任何反应:

override fun onOptionsItemSelected(item: MenuItem?): Boolean {

    if (R.id.action_now == item?.itemId) {
        week = WeekUtils.getCurrentWeek(timetable.entity)
        viewModel.selectedWeekPosition.set(week - 1)

        return true
    }

    return super.onOptionsItemSelected(item)
}

我想由于第一个布局(布局数据活页夹)将其解包为 Int 而第二个布局活页夹得到 Int 而不是 ObservableInt,因此该值不会改变。但我仍然不知道如何修复它不直接在工具栏微调器活页夹中传递 ObservableInt

有人知道如何解决吗? Spinner 绑定适配器如下:

object SpinnerBindings {

    @BindingAdapter("selectedItem")
    @JvmStatic fun Spinner.setSelectedItem(position: Int) {
        setSelection(position)
    }
}

将您的视图模型传递给工具栏微调器布局。

第 1 步:将绑定属性添加到包含 include(片段布局)的布局标签:

xmlns:bind="http://schemas.android.com/apk/res-auto"

第 2 步:更改工具栏微调器布局中的数据项:

<data>

    <variable
        name="vmodel"
        type="com.contedevel.timetable.models.WeekViewModel" />
</data>

第 3 步:更改您的包含标签,如下所示:

<include
            android:id="@+id/toolbar_layout"
            layout="@layout/toolbar_spinner"
             bind:vmodel="@{model}"/>

第 4 步:您可以访问工具栏微调器布局中的 viewModel,因此:

 <Spinner
        android:id="@+id/toolbar_spinner"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:theme="@style/Theme.Paper.Toolbar.Spinner"
        app:popupTheme="@style/Theme.Paper.Toolbar.Spinner.Popup"
        app:selectedItem="@{vmodel.selectedWeekPosition}"/>