android 数据绑定 简介
数据绑定框架给我们带来了很大的方便。减少findViewById方法使用,增加耦合性等等,各种好处。
这两天使用感觉下来,确实提供了一些方便,比如登陆信息,使用双向绑定,代码中就没有对EditText与User,控件和POJO的操作,交给代理去做。但是学习起来也并不容易。每个逻辑书写都要符合一定的lambda的语法规则。特记录下来,方便查找。
官方文档
转义字符
字符 | 转义字符字符 |
---|---|
& | & |
< | < |
> | > |
如:
android:visibility="@{age < 13 ? View.GONE : View.VISIBLE}"
android:text='@{info.stime != null && info.paystate.equalsIgnoreCase("订单结算") ? String.format(@string/report_sales_stime, FormatUtil.getMMDDHHSS(info.getStime())) : ""}'
实现数据导入xml,已登陆页面为例
1.定义登陆类
//单向绑定
public class LoginBean {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
//双向绑定 1
public class LoginBean extends BaseObservable {
private String name;
private String password;
@Bindable
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
notifyPropertyChanged(BR.name);
}
@Bindable
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
notifyPropertyChanged(BR.name);
}
}
//双向绑定 2
public class LoginBean {
public final ObservableField<String> name = new ObservableField<>();
public final ObservableField<String> password = new ObservableField<>();
}
2.定义xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="loginbean" type="com.kunion.taoke.login.LoginBean" />
<variable name="presenter"
type="com.kunion.taoke.login.LoginPresenter" />
</data>
<LinearLayout
style="@style/login_layout_padding"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/login_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/no_name"
android:inputType="text"
android:text="@={loginbean.name}"
/>
<EditText
android:id="@+id/login_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="@string/no_password"
android:inputType="textPassword"
android:text="@={loginbean.password}"
/>
<Button
android:id="@+id/go_login"
style="@style/button_text_max_width"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="40dp"
android:text="@string/login_title"
android:onClick="@{()-> presenter.loginTask(loginbean)}"
/>
</LinearLayout>
</layout>
3.代码中实现绑定
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
LoginFragBinding binding = DataBindingUtil.inflate(inflater, R.layout.login_frag, container, false);
mLoginBean = new LoginBean();
//保存和初始化用户名密码 mLoginBean.name.set(TKApp.SPUtil.getValue("username", ""));
mLoginBean.password.set(TKApp.SPUtil.getValue("password", ""));
//实现绑定 binding.setLoginbean(mLoginBean);
binding.setPresenter((LoginPresenter) mPresenter);
return binding.getRoot();
}
绑定参数传给子xml
通过属性bind
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto">
<data>
<variable name="user" type="com.example.User"/>
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/name"
bind:user="@{user}"/>
</LinearLayout>
</layout>
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto">
<data>
<variable name="user" type="com.example.User"/>
</data>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@{user.name}">
</layout>
未完待续