美文网首页
Data Binding基础用法

Data Binding基础用法

作者: Perston | 来源:发表于2020-05-19 09:10 被阅读0次

1.DataBinding配置
2.DataBinding的使用
3.UI/事件绑定

1.DataBinding配置

  • 在App module-build.gradle中添加下面的代码
    dataBinding {
        enabled = true
    }

2.DataBinding的使用

  • 在原layout文件外套一层标签
  • 可以点中最外层的标签再按Alt+Enter键,再选Convert to data binding layout快速生成!
    activity_demo.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:hint="@string/first_name" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:hint="@string/last_name" />

        <TextView
            android:id="@+id/tv_first_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:gravity="center" />

        <TextView
            android:id="@+id/tv_last_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:gravity="center" />
    </LinearLayout>
</layout>

当xml套了layout层后,会自动生成ActivityDemoBinding类
DemoActivity.java

public class DemoActivity extends AppCompatActivity {
    Employee employee = new Employee("Zhai","Mark");
    ActivityDemoBinding binding;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_demo);
        binding.tvFirstName.setText(employee.firstName);
        binding.tvLastName.setText(employee.lastName);
    }
}

3.UI/事件绑定

activity_demo.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable
            name="employee"
            type="com.mooc.databingdingtest.Employee" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:hint="@string/first_name" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:hint="@string/last_name" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{employee.firstName}"
            android:textSize="24sp"
            android:gravity="center" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{employee.lastName}"
            android:textSize="24sp"
            android:gravity="center" />
    </LinearLayout>
</layout>

DemoActivity.java

public class DemoActivity extends AppCompatActivity {
    Employee employee = new Employee("Zhai","Mark");
    ActivityDemoBinding binding;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_demo);
        binding.setEmployee(employee);
    }
}

事件绑定——分为二种、方法引用、监听器绑定

方法引用——要求和方法名参数一致才行

activity_demo.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable
            name="employee"
            type="com.mooc.databingdingtest.Employee" />
        <variable
            name="presenter"
            type="com.mooc.databingdingtest.Presenter" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:onTextChanged="@{presenter::onTextChanged}"
            android:hint="@string/first_name" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:hint="@string/last_name" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{employee.firstName}"
            android:textSize="24sp"
            android:gravity="center" />

        <TextView
            android:onClick="@{presenter::onClick}"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{employee.lastName}"
            android:textSize="24sp"
            android:gravity="center" />
    </LinearLayout>
</layout>

DemoActivity.java

public class DemoActivity extends AppCompatActivity implements IOnTextChangedListener {
    Employee employee = new Employee("Zhai","Mark");
    ActivityDemoBinding binding;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_demo);
        binding.setEmployee(employee);
        binding.setPresenter(new Presenter(this,this));
    }

    @Override
    public void textChanged(String s) {
        employee.setFirstName(s);
        binding.setEmployee(employee);
    }

    @Override
    public void onClick() {
        Toast.makeText(this,"点到了!",Toast.LENGTH_SHORT).show();
    }
}

Presenter.java

public class Presenter {
    private Activity activity;
    private IOnTextChangedListener listener;
    private TextView textView;

    public Presenter(Activity activity) {
        this.activity = activity;

    }

    public Presenter(Activity activity, IOnTextChangedListener listener) {
        this.activity = activity;
        this.listener = listener;
    }

    public void go(View view){
        Toast.makeText(activity,"你好!",Toast.LENGTH_SHORT).show();
    }

    public void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        listener.textChanged(text.toString());
        // intentionally empty, template pattern method can be overridden by subclasses
    }
    public void onClick(View view){
        listener.onClick();
    }

}

IOnTextChangedListener.java

public interface IOnTextChangedListener {
    public void textChanged(String s);
    public void onClick();
}

监听器绑定

activity_demo.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable
            name="employee"
            type="com.mooc.databingdingtest.Employee" />
        <variable
            name="presenter"
            type="com.mooc.databingdingtest.Presenter" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:onTextChanged="@{presenter::onTextChanged}"
            android:hint="@string/first_name" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:hint="@string/last_name" />
        <!--监听器绑定的语法——表达式语法-->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{employee.firstName}"
            android:onClick="@{()->presenter.onClickListenerBinding(employee)}"
            android:textSize="24sp"
            android:gravity="center" />

        <TextView
            android:onClick="@{presenter::onClick}"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{employee.lastName}"
            android:textSize="24sp"
            android:gravity="center" />
    </LinearLayout>
</layout>

DemoActivity.java

public class DemoActivity extends AppCompatActivity implements IOnTextChangedListener {
    Employee employee = new Employee("Zhai","Mark");
    ActivityDemoBinding binding;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_demo);
        binding.setEmployee(employee);
        binding.setPresenter(new Presenter(this,this));
    }

    @Override
    public void textChanged(String s) {
        employee.setFirstName(s);
        binding.setEmployee(employee);
    }

    @Override
    public void onClick() {
        Toast.makeText(this,"点到了!",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClickListenerBinding(Employee employee) {
        Toast.makeText(this,"点到了!"+ employee.getFirstName(),Toast.LENGTH_SHORT).show();
    }
}

Presenter.java

public class Presenter {
    private Activity activity;
    private IOnTextChangedListener listener;
    private TextView textView;

    public Presenter(Activity activity) {
        this.activity = activity;

    }

    public Presenter(Activity activity, IOnTextChangedListener listener) {
        this.activity = activity;
        this.listener = listener;
    }

    public void go(View view){
        Toast.makeText(activity,"你好!",Toast.LENGTH_SHORT).show();
    }

    public void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        listener.textChanged(text.toString());
        // intentionally empty, template pattern method can be overridden by subclasses
    }
    public void onClick(View view){
        listener.onClick();
    }

    /**
     * 监听器绑定的语法
     * @param employee
     */
    public void onClickListenerBinding(Employee employee){
        listener.onClickListenerBinding(employee);
    }

}

IOnTextChangedListener.java

public class Presenter {
    private Activity activity;
    private IOnTextChangedListener listener;
    private TextView textView;

    public Presenter(Activity activity) {
        this.activity = activity;

    }

    public Presenter(Activity activity, IOnTextChangedListener listener) {
        this.activity = activity;
        this.listener = listener;
    }

    public void go(View view){
        Toast.makeText(activity,"你好!",Toast.LENGTH_SHORT).show();
    }

    public void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        listener.textChanged(text.toString());
        // intentionally empty, template pattern method can be overridden by subclasses
    }
    public void onClick(View view){
        listener.onClick();
    }

    /**
     * 监听器绑定的语法
     * @param employee
     */
    public void onClickListenerBinding(Employee employee){
        listener.onClickListenerBinding(employee);
    }

}

Employee.java

public class Employee {
    public String  firstName;
    public String  lastName;

    public Employee(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

相关文章

网友评论

      本文标题:Data Binding基础用法

      本文链接:https://www.haomeiwen.com/subject/bldvohtx.html