使用
app build.grable
android {
.......
dataBinding{
enabled=true;
}
}
xml外面包裹一层<layout>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
...
</layout>
去除findViewById
binding.firstName.setText("FirstName");
binding.lastName.setText("LastName");
xml中:
<TextView
android:id="@+id/first_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/last_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
变量绑定
public class Employee {
public String firstName;
public String lastName;
public Employee(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
}
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="employee"
type="myapplication3.xt.com.myapplication.Employee"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入firstName" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入lastName" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{employee.firstName}"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{employee.lastName}"
/>
</LinearLayout>
</layout>
public class MainActivity extends AppCompatActivity {
private Employee employee=new Employee("Zhang","San");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding= DataBindingUtil.setContentView(this,R.layout.activity_main);
// binding.setEmployee(employee);
//或
binding.setVariable(BR.employee,employee);
}
}
事件绑定
方法引用
public class MainActivity extends AppCompatActivity {
private Employee employee=new Employee("Zhang","San");
private 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.firstName= s.toString();
binding.setEmployee(employee);
}
}
}
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="employee"
type="myapplication3.xt.com.myapplication.Employee"/>
<variable
name="presenter"
type="myapplication3.xt.com.myapplication.MainActivity.Presenter"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:onTextChanged="@{presenter.onTextChanged}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入firstName" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入lastName" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{employee.firstName}"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{employee.lastName}"
/>
</LinearLayout>
</layout>
onClick(方法引用)
public class Presenter{
public void onTextChanged(CharSequence s, int start, int before, int count){
employee.firstName= s.toString();
binding.setEmployee(employee);
}
public void onClick(View view){
Toast.makeText(MainActivity.this, "点到了", Toast.LENGTH_SHORT).show();
}
}
<TextView
android:onClick="@{presenter.onClick}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{employee.firstName}"
/>
监听器绑定
public class Presenter{
public void onTextChanged(CharSequence s, int start, int before, int count){
employee.firstName= s.toString();
binding.setEmployee(employee);
}
public void onClick(View view){
Toast.makeText(MainActivity.this, "点到了", Toast.LENGTH_SHORT).show();
}
public void onClickListenerBinding(Employee employee){
Toast.makeText(MainActivity.this, employee.lastName, Toast.LENGTH_SHORT).show();
}
}
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{employee.lastName}"
android:onClick="@{()->presenter.onClickListenerBinding(employee)}"
/>