一、认识DataBinding
DataBinding,2015年IO大会介绍的一个框架,字面理解即为数据绑定,是Google对MVVM在Android上的一种实现,可以直接绑定数据到xml中,并实现自动刷新。
好处:
去掉大部分UI相关代码(比如findViewById、setOnClickListener、setText等)
xml变成UI的唯一真实来源,数据绑定也直接发生在xml
二、DataBinding使用配置
1.环境要求
AndroidStudio1.3以上
gradle插件1.5以上
2.gradle配置
在android中添加:
dataBinding {
enabled = true
}
3.在xml布局文件中添加layout标签
<layout>
// 原来的layout
</layout>
4.Binding自动生成规则
默认生成规则:xml通过文件名生成,使用下划线分割大小写。
比如activity_demo.xml,则会生成ActivityDemoBinding,item_search_hotel则会生成ItemSearchHotelBinding。也可以自定义生成的class名字,在data中指定class:
<data class=“MyClassName”>
…
</data>
view的生成规则类似,只是由于是类变量,首字母不是大写,比如有一个TextView的id是first_name,则会生成名为firstName的TextView。
使用方法:
1,设置布局文件的值:
Activity中
Employee employee = new Employee("da 神", "Mark 哈哈");
ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
binding.tvFirstName.setText(employee.getFirstname());
binding.tvLastName.setText(employee.getLastname());
2.UI/和事件的绑定
XML布局中
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="employee"
type="mooc.com.animtest.Employee"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入First Name"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入 Last Name"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{employee.firstname}"
android:textSize="22sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{employee.lastname}"
android:textSize="22sp"/>
</LinearLayout>
</layout>
Activty中代码:
Employee employee = new Employee("安卓da 神", " 哈哈 傻子");
ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.setEmployee(employee);
}
事件绑定:
android:onClick
android:onLongClick
android:onTextChanged
public class MainActivity extends AppCompatActivity {
Employee employee = new Employee("安卓da 神", " 哈哈 傻子");
ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.setEmployee(employee);
binding.setPresenter(new Presenter());
}
public class Presenter {
public void onTextChanged(CharSequence s, int start, int before, int count) {
employee.setFirstname(s.toString());
binding.setEmployee(employee);
}
public void onClick(View view) {
Toast.makeText(MainActivity.this, "点到了" + view.getId(), Toast.LENGTH_SHORT).show();
}
//监听器绑定
public void onClickListenerBinding(Employee employee) {
Toast.makeText(MainActivity.this, employee.getLastname(), Toast.LENGTH_SHORT).show();
}
}
}
xml布局文件
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="employee"
type="mooc.com.animtest.Employee"/>
<variable
name="presenter"
type="mooc.com.animtest.MainActivity.Presenter"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入First Name"
android:onTextChanged="@{presenter.onTextChanged}"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入 Last Name"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="@{presenter.onClick}"
android:text="@{employee.firstname}"
android:textSize="22sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="@{()->presenter.onClickListenerBinding(employee)}"
android:text="@{employee.lastname}"
android:textSize="22sp"/>
</LinearLayout>
</layout>
基本原理:
android.binding;
BR
XxxBinding