定义BindingAdapter
代码语言:javascript复制public class MyBindingAdapter {
//
@BindingAdapter("image")
public static void setImage(ImageView imageView, String url){
}
}
布局里处理
代码语言:javascript复制//data里加上变量名字
<data>
<variable
name="networkImage"
type="String" />
<import type="com.example.databinding.IdolUtils"/>
</data>
//view上使用
<ImageView
android:id="@ id/imageview"
android:layout_width="300dp"
android:layout_height="300dp"
//这里的image就是BindingAdapter里@BindingAdapter注解后面的变量名
//networkImage就是上面布局里定义的
app:image="${networkImage}"
app:layout_constraintBottom_toBottomOf="@ id/include"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/button" />
activity里传入图片的链接
代码语言:javascript复制@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);
//设置data标签里的变量的值
//这个值会经布局,传给BindingAdapter处理
activityMainBinding.setNetworkImage("网络图片地址");
}
BindingAdapter中同时定义多个参数
代码语言:javascript复制//requireAll = false表示2个值不需要都有值
@BindingAdapter(value = {"image", "localImage"},requireAll = false)
public static void setImage(ImageView imageView, String url, int resId){
}
绑定RecyclerView
主要是adapter
代码语言:javascript复制public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{
List<User> list;
public MyAdapter(List<User> list) {
this.list = list;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
ItemBinding itemBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
R.layout.item,
parent,
false);
return new MyViewHolder(itemBinding);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
User user = list.get(position);
holder.itemBinding.setUser(user);
}
@Override
public int getItemCount() {
return list.size();
}
static class MyViewHolder extends RecyclerView.ViewHolder{
private ItemBinding itemBinding;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
}
public MyViewHolder(ItemBinding itemBinding) {
super(itemBinding.getRoot());
this.itemBinding = itemBinding;
}
}
}
布局绑定主要是在子布局中
代码语言:javascript复制//item.xml
<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="user"
type="com.example.databinding3.User" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@ id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:image="@{user.image}"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@tools:sample/avatars" />
<TextView
android:id="@ id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="TextView"
android:text="@{user.name}"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@ id/imageView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.179"
app:layout_constraintStart_toEndOf="@ id/imageView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.512" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
图片的加载还是参考之前的用 @BindingAdapter
码字不易,求转发,求点在看,求关注,感谢!