Установка переменной customView при привязке данных

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

attribute buttonText (aka com.example.testbinding:buttonText) not found. error: failed linking file resources.

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>

В этом сообщение я нашел:

In your Custom View, inflate layout however you normally would and provide a setter for the attribute you want to set:

В моем классе 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?

1
0
146
2

Ответы 2

binding.setbuttonText("text_button");

вместо

    public String getButtonText() {
        return buttonText;
    }

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

Спасибо, но проблема здесь: <com.example.testbinding.CustomView android:id = "@+id/customView" ... app:buttonText = "Текст кнопки тестирования" ... > Я не могу установить это свойство из родителя XML.

Andrey Zyritel 12.03.2019 11:34

можете ли вы попытаться установить значение в java-файле, используя binding.setbuttonText("text_button"); вместо того, чтобы писать код в xml-файле? @AndreyZyritel

Dhara Jani 19.06.2019 14:52

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

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

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

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 должен иметь определенный установщик для этих свойств.

Другие вопросы по теме