Привязка данных Android <include>привязка тега с ViewModel не работает - PullRequest
0 голосов
/ 16 декабря 2018

Я хочу включить пользовательский макет xml в основной макет и связать пользовательский viewModel с пользовательским xml, но он не работает. Значение в viewModel не отображается в xml.Вот мои коды: Main.xml:

<layout>

<data>

    <import type="com.example.databindingpractise.upDownchoiceWidget.UpDownChoiceViewModel"/>
    <variable
        name="upDownviewModel"
        type="UpDownChoiceViewModel"/>
</data>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <include
        android:id="@+id/upanddown"
        app:upDownviewModel="@{upDownviewModel}"
        layout="@layout/up_and_down_choice_layout"/>

</LinearLayout>

up_and_down_choice_layout.xml:

<layout >

<data>

    <import type="com.example.databindingpractise.upDownchoiceWidget.UpDownChoiceViewModel"/>

    <variable
        name="upDownviewModel"
        type="UpDownChoiceViewModel"/>
</data>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/order_around_shadow">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="14dp"
        android:text="@{upDownviewModel.upLocation}"/>



</LinearLayout>

MainActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);

    LayoutInflater layoutInflater = LayoutInflater.from(this);

    UpAndDownChoiceLayoutBinding upAndDownChoiceLayoutBinding = DataBindingUtil.inflate(layoutInflater, R.layout.up_and_down_choice_layout, null, false);

    UpDownChoiceViewModel upDownChoiceViewModel = new UpDownChoiceViewModel();
    upAndDownChoiceLayoutBinding.setUpDownviewModel(upDownChoiceViewModel);

}

Как связать upDownChoiceViewModel с up_and_down_choice_layout.xml, может кто-нибудь решить мой вопрос 101

Ответы [ 2 ]

0 голосов
/ 16 декабря 2018

Ваш код должен выглядеть следующим образом

    <layout>

<data>

    <variable
        name="upDownviewModel"
        type="com.example.databindingpractise.upDownchoiceWidget.UpDownChoiceViewModel"/>
</data>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <include
        android:id="@+id/upanddown"
        app:upDownviewModel="@{upDownviewModel}"
        layout="@layout/up_and_down_choice_layout"/>

</LinearLayout>

up_and_down_choice_layout

<layout >

<data>

    <variable
        name="upDownviewModel"
        type="com.example.databindingpractise.upDownchoiceWidget.UpDownChoiceViewModel"/>
</data>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/order_around_shadow">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="14dp"
        android:text="@{upDownviewModel.upLocation}"/>



</LinearLayout>
0 голосов
/ 16 декабря 2018

Используйте, как показано ниже.

<data>       

    <variable
        name="upDownviewModel"
        type="com.example.databindingpractise.upDownchoiceWidget.UpDownChoiceViewModel" />
</data>
...