在 android 上进行双向数据绑定的正确方法是什么?
What is the correct way to do two-way data binding on android?
我为 2 种方式的数据绑定制作了一个简单的 hello world 并且接缝工作完美(当在 editext 上编写时,textview 会自动更新),但是在线找到的所有代码(如官方文档)都有更多的代码和复杂性,例如 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>
在特定的文档中使用了这个东西但是 seams 没用:
- BaseObservable
- @可绑定
- 避免无限循环的代码
- notifyPropertyChanged
那么,我的代码有什么错误或缺失?
在双向数据绑定中,您需要创建从 BaseObservable
扩展的 class,使用 @Bindable
注释 getter 并在您的 setter 中调用 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);
}
}
然后将此 class 作为 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
属性中的 class 路径。
然后在 activity 中使用 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(""));
}
}
对我来说最简单的方法是使用@={variable} 代替@{variable}
您可以在以下位置看到它:
https://developer.android.com/topic/libraries/data-binding/two-way
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@={viewModel.name}"
android:inputType="text" />
我为 2 种方式的数据绑定制作了一个简单的 hello world 并且接缝工作完美(当在 editext 上编写时,textview 会自动更新),但是在线找到的所有代码(如官方文档)都有更多的代码和复杂性,例如 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>
在特定的文档中使用了这个东西但是 seams 没用:
- BaseObservable
- @可绑定
- 避免无限循环的代码
- notifyPropertyChanged
那么,我的代码有什么错误或缺失?
在双向数据绑定中,您需要创建从 BaseObservable
扩展的 class,使用 @Bindable
注释 getter 并在您的 setter 中调用 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);
}
}
然后将此 class 作为 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
属性中的 class 路径。
然后在 activity 中使用 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(""));
}
}
对我来说最简单的方法是使用@={variable} 代替@{variable}
您可以在以下位置看到它: https://developer.android.com/topic/libraries/data-binding/two-way
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@={viewModel.name}"
android:inputType="text" />