databinding
一、使用
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<!--ZSG xml里绑定和使用bean-->
<data>
<variable
name="user"
type="com.android.databind.bean.User" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv1"
android:textSize="100sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{user.name}" />
<TextView
android:id="@+id/tv2"
android:textSize="100sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{user.age}" />
</LinearLayout>
</layout>
package com.android.databind;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import com.android.databind.bean.User;
import com.android.databind.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding viewDataBinding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
User zhangsan = new User("zhangsan", "18");
viewDataBinding.setUser(zhangsan);
new Thread(){
@Override
public void run() {
for (int i = 0; i < 8; i++) {
zhangsan.setAge(zhangsan.getAge() +i);
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
}
package com.android.databind.bean;
import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
import androidx.databinding.library.baseAdapters.BR;
public class User extends BaseObservable {
private String name;
private String age;
public User(String name, String age) {
this.name = name;
this.age = age;
}
@Bindable
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
notifyPropertyChanged(BR.name);
}
@Bindable
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
notifyPropertyChanged(BR.age);
}
}
二、kotlin 使用databinding
package com.test.mydatabinding.model
import androidx.databinding.ObservableField
class Student {
val nameK : ObservableField<String> by lazy { ObservableField<String>() }
val ageK : ObservableField<Int> by lazy { ObservableField<Int>() }
}
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="student"
type="com.test.mydatabinding.model.Student" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{student.nameK}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{student.ageK.toString()}"
app:layout_constraintTop_toBottomOf="@+id/tv1"
app:layout_constraintStart_toStartOf="@+id/tv1"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
package com.test.mydatabinding
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import com.test.mydatabinding.databinding.ActivityMainBinding
import com.test.mydatabinding.model.Student
class MainActivity : AppCompatActivity() {
private var student = Student()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
var binding =
DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
student.nameK.set("zhangsan")
student.ageK.set(18)
binding.student = student
}
}
三、原理 TODO