Android官方DataBinding(四):BindingAdapter
(1)和附录1,2,3一样,毫无例外,先写一个数据模型User.java:
package zhangphil.test;
import android.databinding.BaseObservable;
import android.databinding.ObservableField;
import android.databinding.ObservableInt;
/**
* Created by Phil on 2017/8/17.
*/
public class User extends BaseObservable {
public final ObservableInt id = new ObservableInt();
public final ObservableField<String> name = new ObservableField<>();
public final ObservableField<String> url = new ObservableField<>();
}
(2)写activity_main.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">
<data>
<variable
name="user"
type="zhangphil.test.User" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{String.valueOf(user.id)}"
android:textColor="@android:color/holo_red_light" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.name}"
android:textColor="@android:color/holo_red_light" />
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
app:imageUrl="@{user.url}" />
</LinearLayout>
</layout>
注意这里ImageView的属性配置。
(3)任意定义一个类,比如叫做Util.java,在这个类里面的方法通过BindingAdapter,imageUrl和loadImage方法绑定:
package zhangphil.test;
import android.databinding.BindingAdapter;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
/**
* Created by Phil on 2017/8/18.
*/
public class Util {
@BindingAdapter({"imageUrl"})
public static void loadImage(ImageView view, String u) {
RequestOptions options = new RequestOptions()
.centerCrop()
.placeholder(R.mipmap.ic_launcher_round)
.error(R.mipmap.ic_launcher)
.priority(Priority.HIGH)
.diskCacheStrategy(DiskCacheStrategy.NONE);
Glide.with(view.getContext()).applyDefaultRequestOptions(options).load(u).transition(new DrawableTransitionOptions().crossFade(1000)).into(view);
}
}
(注:Glide是v4.0.0)
(4)上层MainActivity.java:
package zhangphil.test;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import zhangphil.test.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
User user = new User();
binding.setUser(user);
user.id.set(1);
user.name.set("zhangphil");
//user.url.set("http://avatar.csdn.net/9/7/A/1_zhangphil.jpg");
user.url.set("https://www.baidu.com/img/bd_logo1.png");
}
}
代码运行结果:
在activity_main.xml里面的ImageView,由于定义了app:imageUrl="@{user.url}"
这使得user.url这个数据字段成为方法imageUrl的被观察者,BindingAdapter两者绑定在一起,user.url的动态变化,直接导致imageUrl的调用。同时,user.url将作为一个字符串变量传递给imageUrl方法,在imageUrl方法里面,反向的将Glide加载的图片赋值给绑定的ImageView。
附录:
1,《Android官方DataBinding简例(一)》链接:http://blog.csdn.net/zhangphil/article/details/77322530
2,《Android官方DataBinding(二):动态数据更新notifyPropertyChanged》链接:http://blog.csdn.net/zhangphil/article/details/77328688
3,《Android官方DataBinding(三):RecyclerView 使用ViewDataBinding更新数据》链接:http://blog.csdn.net/zhangphil/article/details/77367432