1.配置 build.gradle 开启databinding
dataBinding {
enabled = true
}
2.xml新增layout data variable然后build回主界面 LayoutActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.layout_activity_main);
<?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">
<data>
<variable
name="user"
type="com.lei.bindview.User"/>
<variable
name="progress"
type="com.lei.bindview.Progress"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text='@{user.name,default="name"}'/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text='@{user.age+@string/percent,default="age"}'/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:imageUrl="@{user.picUrl}"/>
<com.lei.bindview.LandleafView
app:progress='@{progress}'
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cBoundSize="3"
app:cLevelText="舒适"
app:cLevelTextSize="18"
app:cLineWidth="12"
app:cMaxText="30"
app:cMinText="0"
app:cProgressText="24"
app:cProgressTextSize="75"
app:cRadius="115"
app:cRangeTextSize="15"
app:cTriangleWidth="12"
/>
</LinearLayout>
</layout>
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.layout_activity_main);
User user = new User("zn", "25","http://www.landleafservice.cn:8080/pics/icon_cloudy.png");
Progress progress = new Progress(20, 0, 36, Color.GREEN, "优");
activityMainBinding.setUser(user);
activityMainBinding.setProgress(progress);
}
@BindingAdapter("imageUrl")
public static void bindImage(ImageView imageView, String url){
Log.d("ImageLoadAdaptersss", url);
Picasso.with(imageView.getContext()).load(url).into(imageView);
}
}
坑1变量引入
<data>
<import type="com.landleaf.httpplugin.bean.WeatherModel"/>
<import type="com.landleaf.httpplugin.bean.Yinchaun"/>
<variable
name="weatherModel"
type="WeatherModel" />
<variable
name="yinchuan"
type="Yinchaun"/>
</data>
坑2设置默认值&使用引用value属性
android:text='@{yinchuan.voc+@string/tv_unit_mg,default="暂无"}'
坑3图片绑定
//绑定一个下载图片的方法:需要图片URL和ImageView来显示,让这个方法能直接调用,最简单的就先定义为static 可放在Activity中或者新开一个class丢进去,会自动调用,照格式写就行
@BindingAdapter({"imageUrl"})
public static void loadImage(ImageView view, String url) {
Glide.with(view.getContext()).load(url).into(view);
}
app:imageUrl='@{weatherModel.picUrl}'
坑4绑定自定义控件
<com.lei.bindview.LandleafView
app:progress="@{progress}"
public void setProgress(Progress pro) {} 单属性设置,此处为单参数
//多属性需要自定义
@BindingAdapter({"bind:imageUrl", "bind:error"})
public static void loadImage(ImageView view, String url, Drawable error) {
Picasso.with(view.getContext()).load(url).error(error).into(view);
}
<ImageView
app:imageUrl=“@{venue.imageUrl}”
app:error=“@{@drawable/venueError}”/>
坑5绑定点击事件
<variable
name="onClickListener"
type="android.view.View.OnClickListener" />
binding.setOnClickListener(this);
@Override
public void onClick(View v) {
switch (v.getId()) {}
}
留作纪念。。。