在android上做双向数据绑定的正确方法是什么?

问题描述 投票:0回答:1

我做了一个简单的hello world的双向数据绑定,缝隙工作完美(当写在editext上时,文本视图自动更新),但所有在线找到的代码,如官方文档有更多的代码和复杂的问题,如 https:/developer.android.comtopiclibrariesdata-bindingtwo-way。

这是我的代码。

public class MainActivity extends AppCompatActivity {
    public String pippo;
    public Boolean visible = true;

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

    }
}



<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

        <variable
            name="pippo"
            type="String" />

        <variable
            name="visible"
            type="Boolean" />
    </data>

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

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@={pippo}" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{pippo}"
            android:visibility="@{visible ? android.view.View.VISIBLE: android.view.View.GONE}" />

        <CheckBox
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:checked="@={visible}" />
    </LinearLayout>
</layout>

在特定的文档中使用了这个东西,但似乎没有用。

  • BaseObservable
  • @Bindable
  • 避免无限循环的代码
  • notifyPropertyChanged

那么,我的代码有什么问题或缺失?

android android-databinding
1个回答
2
投票

在双向数据绑定中,你需要创建一个类,该类扩展自 BaseObservable,用 @Bindable 并称 notifyPropertyChanged 在你的设置器中,如下所示。

public class Person extends BaseObservable {

    private String name;

    Person(String name) {
        this.name = name;
    }

    @Bindable
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        notifyPropertyChanged(BR.name);
    }

}

然后把这个类作为一个数据绑定布局变量,类型为 Person.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

        <variable
            name="person"
            type="com.example.android......Person" />
    </data>

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

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@={person.name}" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{person.name}" />
    </LinearLayout>
</layout>

注意:改变类路径中的 type 属性,然后在你的活动中设置这个布局变量。

然后在你的活动中设置这个布局变量,用 setPerson()

public class ExampleActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DataBindingUtil.setContentView(this, R.layout.activity_example);

        ActivityExampleBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_example);

        mActivityMainBinding.setPerson(new Person(""));
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.