установка переменной customView при привязке данных - PullRequest
1 голос
/ 12 марта 2019

Я пытаюсь установить переменную (buttonText) customView из main_activity.xml, но я получил эту ошибку:

атрибут buttonText (он же com.example.testbinding: buttonText) не найден.ошибка: не удалось связать файловые ресурсы.

customView.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>
       <variable
            name="buttonText"
            type="String" />
    </data>

    <LinearLayout
        android:id="@+id/container"
        android:orientation="vertical">

        <Button
            android:id="@+id/button"
            android:text="@{buttonText}" />
    </LinearLayout>
</layout>

main_activity.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>
    </data>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <com.example.testbinding.CustomView
            android:id="@+id/customView"
            ...
            app:buttonText = "Test button text"
            ...
        >

        </com.example.testbinding.CustomView>
    </android.support.constraint.ConstraintLayout>
</layout>

В этом сообщении Iнайдено:

В пользовательском представлении раздуте макет, как обычно, и предоставьте установщик для атрибута, который вы хотите установить:

Мой класс CustomView имеет общедоступный установщикдля свойства buttonText:

    public class CustomView extends LinearLayout {

        private CustomViewBinding binding;
        private String buttonText;

        public CustomView(Context context) {
            super(context);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            binding = DataBindingUtil.inflate(inflater, R.layout.custom_view, this, true);
        }

        public String getButtonText() {
            return buttonText;
        }

        public void setButtonText(String buttonText) {
            this.buttonText = buttonText;
        }
    }

Может кто-нибудь сказать мне, что с ним не так?Или это невозможно без BindingAdapter?

Ответы [ 2 ]

0 голосов
/ 14 марта 2019

Кажется, я нашел, что было не так.

Если я использую переменную, объявленную в родительском представлении - все в порядке.Но если я использую правильное значение в свойстве, оно не будет работать.

неправильно:

app:buttonText = "Test button text"
app:progressValue = "50"

правильно:

<variable
name="viewModel"
type="com.example.testbinding.ViewModel"/>

app:buttonText = "@{viewModel.buttonText}"
app:progressValue = "@{viewModel.progressValue}"

Конечно, ViewModel должен иметьопределенный установщик для этих свойств.

0 голосов
/ 12 марта 2019

binding.setbuttonText ( "text_button");

вместо

    public String getButtonText() {
        return buttonText;
    }

    public void setButtonText(String buttonText) {
        this.buttonText = buttonText;
    }
...