AndroidDataBinding的简单使⽤,@BindingAdapter通过xml传⼊参数
导⼊
同时开启dataBinding和viewBinding
android {
...
buildFeatures {
dataBinding =true
// for view binding :
viewBinding =true
}
...
}
或者
dataBinding {
enabled =true
}
转换成databinding布局
在kotlin中xml布局的跟节点必须是layout(不能FrameLayout等),否则会⽣成ViewBinding类⽽不是ViewDataBinding类。可以使⽤Alt+Enter来快速转换成databinding的布局
xml
<?xml version="1.0" encoding="utf-8"?>
<layout android="schemas.android/apk/res/android"
app="schemas.android/apk/res-auto"
tools="schemas.android/tools">
<data>
<variable
name="user"
type="jetpack.bean.User"/>
</data>
<LinearLayout
layout_width="match_parent"
layout_height="match_parent"
orientation="vertical"
context=".Main3Activity">
<Button
id="@+id/bt_test_1"
layout_width="match_parent"
layout_height="wrap_content"
onClick="onClick"
text="@{user.name}"/>
<Button
id="@+id/bt_test_2"
layout_width="match_parent"
layout_height="wrap_content"
text="@{String.valueOf(user.age)}"/>
<Button
id="@+id/bt_test_3"
layout_width="match_parent"
layout_height="wrap_content"
text="@{user.sex}"/>
<jetpack.view.BindingImageView
id="@+id/iv_img"
layout_width="wrap_content"
layout_height="wrap_content"
layout_gravity="center"
isCircle="@{true}"
imgUrl="@{user.portrait}"/>
</LinearLayout>
</layout>
⾃定义BindingImageView
通过BindingAdapter可以通过xml传递属性给⾃定义view
public class BindingImageView extends AppCompatImageView {
public BindingImageView(@NonNull Context context){
super(context);
}
public BindingImageView(@NonNull Context context,@Nullable AttributeSet attrs){
super(context, attrs);
}
public BindingImageView(@NonNull Context context,@Nullable AttributeSet attrs,int defStyleAttr){
super(context, attrs, defStyleAttr);
}
//requireAll如果有多个参数是否需要全部传⼊
@BindingAdapter(value ={"imgUrl","isCircle"},requireAll =false)
public static void setImageUrl(BindingImageView view, String imgUrl,boolean isCircle){
if(isCircle){
Glide.with(view).load(imgUrl).circleCrop().into(view);
}else{
Glide.with(view).load(imgUrl).into(view);
}
}
}
MainActivity
onClick 中给age加1,数据会⾃动更新到UI上
public class Main2Activity extends AppCompatActivity implements LifecycleOwner {
private ActivityMain2Binding binding;
@Override
protected void onCreate(Bundle savedInstanceState){
binding = DataBindingUtil.setContentView(this, R.layout.activity_main2);
android简单教程binding.setUser(new User("www.baidu/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png","zy",10,"男")); }
public void onClick(View view){
int age = User().getAge();
}
}
Kotlin中需要注意
⾃定义View中,需要有⼆级构造函数,让AttributeSet属性可以传⼊
class EditorBottomNavigation: LinearLayout {
constructor(context: Context):this(context,null,0)
constructor(context: Context, attrs: AttributeSet?):this(context, attrs,0)
constructor(context: Context, attrs: AttributeSet?, defStyle: Int):super(
context,
attrs,
defStyle
)
}
以下这种只有主构造函数是不能使⽤BindingAdapter的,因为没有AttributeSet可以设置
class ImageCropView(
context: Context,
attrs: AttributeSet?=null,
defStyleAttr: Int =0
):
AppCompatImageView(context, attrs, defStyleAttr){
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。