在Android上进行双向数据绑定的正确方法是什么? [英] What is the correct way to do two-way data binding on android?
问题描述
我为2种方式的数据绑定和接缝创建了一个简单的hello世界(当在editext上编写时,textview自动更新),但是在网上找到的所有代码(如官方文档)都具有更多的代码和复杂性,如
i made a simple hello world for 2 way data binding and seams works perfectly (when write on editext, the textview update automatically), but all code found online like official documentation has much more code and complications like https://developer.android.com/topic/libraries/data-binding/two-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>
特别是文档使用了这些东西,但是接缝无用:
In particular documentation use this things but seams useless:
- BaseObservable
- @可绑定
- 避免无限循环的代码
- notifyPropertyChanged
那么,我的代码有什么错误或遗漏?
so, what wrong or missing with my code?
推荐答案
在双向数据绑定中,您需要创建从 BaseObservable
扩展的类,并使用 @Bindable
,然后在设置器中调用 notifyPropertyChanged
,如下所示:
In the two-way data binding you need to create class that extends from BaseObservable
, annotate getters with @Bindable
and call notifyPropertyChanged
in your setters as below:
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
的数据绑定布局变量传递.
Then pass this class as a databinding layout variable of type 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
属性中的类路径.
Note: change the class path in the type
attribute.
,然后在您的活动中使用 setPerson()
and then set this layout variable in your activity with 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(""));
}
}
这篇关于在Android上进行双向数据绑定的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!