字面意思数据绑定 优点:灵活解耦,避免编写重复性的模板代码 缺点:方法调用在xml中进行,不进行编译检查,运行时才检查,比如需要传int,我传string照样不报错 (一)直接在布局文件中操作类中的数据!
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="85dp"
android:layout_marginEnd="4dp"
android:layout_marginRight="4dp"
android:text="@{StarUtils.getStar(ido.star)}"
app:layout_constraintEnd_toEndOf="@+id/textView"
app:layout_constraintTop_toBottomOf="@+id/textView" />
1在module的build.gradle中配置;需要rebuild构建一下
dataBinding{
enabled=true
}
android {
compileSdkVersion 29
defaultConfig {
applicationId "com.dongnaoedu.jetpackstudy"
minSdkVersion 27
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
dataBinding{
enabled=true
}
}
2布局文件 中alt+回车,切换成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">
<data>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DataBindingActivity">
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
3通过variable标签绑定类与布局文件
package com.dongnaoedu.jetpackstudy;
public class Ido {
public String name;
public String star;
public Ido(final String mName, final String mStar) {
name = mName;
star = mStar;
}
}
此布局文件通过@{}直接调用类中的属性赋值
<?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="ido"
type="com.example.databinding.Ido" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.databinding.DataBindingActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="139dp"
android:layout_marginEnd="163dp"
android:layout_marginRight="163dp"
android:text="@{ido.name}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="85dp"
android:layout_marginEnd="4dp"
android:layout_marginRight="4dp"
android:text="@{ido.star}"
app:layout_constraintEnd_toEndOf="@+id/textView"
app:layout_constraintTop_toBottomOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
DataBinding在Activity中的使用
public class DataBindingActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//注意:ActivityDataBinding与activity_data_binding一一对应
ActivityDataBindingBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_data_binding);
Ido ido = new Ido("wy", "五星");
activityMainBinding.setIdo(ido);
}
}
(二)点击监听事件的实现 1创建事件监听回调机制
public class EventHandleListener {
private Context context;
public EventHandleListener(Context context) {
this.context = context;
}
public void buttonOnClick(View view){
Log.e("wy", "buttonOnClick: " );
Toast.makeText(context,"喜欢",Toast.LENGTH_SHORT).show();
}
}
2xml布局中做关联
<variable
name="eventHandle"
type="com.example.databinding.EventHandleListener" />
设置具体控件点击事件
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="122dp"
android:layout_marginRight="122dp"
android:layout_marginBottom="329dp"
android:onClick="@{eventHandle.buttonOnClick}"
android:text="like"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
xml全代码
<?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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.databinding.DataBindingActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="139dp"
android:layout_marginEnd="163dp"
android:layout_marginRight="163dp"
android:layout_marginBottom="198dp"
android:text="@{ido.name}"
tools:text="李狗娃"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="85dp"
android:layout_marginEnd="4dp"
android:layout_marginRight="4dp"
tools:text="五星"
android:text="@{StarUtils.getStar(ido.star)}"
app:layout_constraintEnd_toEndOf="@+id/textView"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="122dp"
android:layout_marginRight="122dp"
android:layout_marginBottom="329dp"
android:onClick="@{eventHandle.buttonOnClick}"
android:text="like"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>
<data>
<variable
name="ido"
type="com.example.databinding.Ido" />
<variable
name="eventHandle"
type="com.example.databinding.EventHandleListener" />
<import type="com.example.databinding.StarUtils"/>
</data>
</layout>
3最重要的一步设置监听事件(传上下文)
activityMainBinding.setEventHandle(new EventHandleListener(this));
public class DataBindingActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_data_binding);
ActivityDataBindingBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_data_binding);
Ido ido = new Ido("wy", 5);
activityMainBinding.setIdo(ido);
activityMainBinding.setEventHandle(new EventHandleListener(this));
}
}
|