DataBinding(数据绑定)基础讲解
非Androidx项目DataBinding 基础讲解_jianning-wu的博客-CSDN博客
DataBinding(数据绑定)ListView使用
DataBinding(数据绑定)ListView使用_jianning-wu的博客-CSDN博客
本章节讲述ImageView中使用DataBinding。
一.绑定单张ImageView
布局文件
<?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="imageUrl"
type="String" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:ignore="ExtraText">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
app:image="@{imageUrl}" />
</LinearLayout>
</layout>
图片显示工具类
package com.wjn.okhttpmvpdemo.databinding;
import android.databinding.BindingAdapter;
import android.util.Log;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
public class ImageUtil {
/**
* 方法一定是静态的 两个参数
*/
@BindingAdapter({"bind:image"})
public static void imageLoader(ImageView imageView, String url) {
Glide.with(imageView.getContext()).load(url).into(imageView);
Log.d("TAG", "url----:" + url);
}
}
Activity使用
package com.wjn.okhttpmvpdemo.databinding;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.wjn.okhttpmvpdemo.R;
public class PictureActivity extends AppCompatActivity {
private ActivityPictureBinding mActivityPictureBinding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivityPictureBinding = DataBindingUtil.setContentView(this, R.layout.activity_picture);
initView();
}
/**
* 初始化View
*/
private void initView() {
mActivityPictureBinding.setImageUrl("https://profile-avatar.csdnimg.cn/19a6b7b495394ec0a2609f12bdae3235_weixin_37730482.jpg");
}
}
二.RecyclerView中使用DataBinding
Activity
public class RecyclerViewDataBindingActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private RecycleViewDataBindingAdapter mRecycleViewAdapter;
private List<DataBindingBean> mList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_databinding_recyclerview);
initView();
}
/**
* 初始化各种View
*/
private void initView() {
mRecyclerView = findViewById(R.id.activity_databinding_recyclerview_recyclerView);
mList = getList();
//1.设置LinearLayoutManager ListView
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//2.设置固定大小
mRecyclerView.setHasFixedSize(true);
//3.设置Adapter
mRecycleViewAdapter = new RecycleViewDataBindingAdapter(this, mList);
mRecyclerView.setAdapter(mRecycleViewAdapter);
}
/**
* 初始化集合数据
*/
private List<DataBindingBean> getList() {
List<DataBindingBean> list = new ArrayList<>();
DataBindingBean listViewBean1 = new DataBindingBean();
listViewBean1.ava = "http://sss";
listViewBean1.title = "张三";
DataBindingBean listViewBean2 = new DataBindingBean();
listViewBean2.ava = "https://avatar.csdnimg.cn/1/2/4/3_weixin_37730482_1607556601.jpg";
listViewBean2.title = "韦德";
DataBindingBean listViewBean3 = new DataBindingBean();
listViewBean3.ava = "http://sss";
listViewBean3.title = "保罗";
for (int i = 0; i < 5; i++) {
list.add(listViewBean1);
list.add(listViewBean2);
list.add(listViewBean3);
}
return list;
}
}
Activity布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/activity_databinding_recyclerview_recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
Adapter
public class RecycleViewDataBindingAdapter extends RecyclerView.Adapter<RecycleViewDataBindingAdapter.ViewHolder> {
private final List<DataBindingBean> mList;
private final LayoutInflater mInflater;
public RecycleViewDataBindingAdapter(Activity activity, List<DataBindingBean> list) {
mList = list;
mInflater = LayoutInflater.from(activity);
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
DatabindingRecyclerviewItemBinding itemBinding = DataBindingUtil.inflate(mInflater, R.layout.databinding_recyclerview_item, parent, false);
return new ViewHolder(itemBinding);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.getBinding().setDatabinding(mList.get(position));
holder.getBinding().setRecyclerViewItemClick(new RecyclerViewItemClickMethod(position));
holder.getBinding().executePendingBindings();
}
@Override
public int getItemCount() {
return mList.size();
}
/**
* ViewHolder类
*/
static class ViewHolder extends RecyclerView.ViewHolder {
private final DatabindingRecyclerviewItemBinding binding;
public ViewHolder(DatabindingRecyclerviewItemBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
public DatabindingRecyclerviewItemBinding getBinding() {
return this.binding;
}
}
/**
* 点击事件
*/
public static class RecyclerViewItemClickMethod {
private final int position;
public RecyclerViewItemClickMethod(int position) {
this.position = position;
}
//TextView点击事件
public void textViewClick(View view) {
if (null == view) return;
Log.d("RecycleView", "TextView点击事件----:" + position);
}
//ImageView点击事件
public void imageViewClick(View view) {
if (null == view) return;
Log.d("RecycleView", "ImageView点击事件----:" + position);
}
}
}
Item布局
<?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="databinding"
type="com.wjn.networkdemo.recyclerview.databinding.DataBindingBean" />
<variable
name="RecyclerViewItemClick"
type="com.wjn.networkdemo.recyclerview.databinding.RecycleViewDataBindingAdapter.RecyclerViewItemClickMethod" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="90dp">
<ImageView
android:id="@+id/databinding_recyclerview_item_imageview"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="10dp"
android:onClick="@{RecyclerViewItemClick::imageViewClick}"
android:scaleType="fitXY"
android:src="@drawable/image_default"
app:avaError="@{@drawable/image_default}"
app:avaImageUrl="@{databinding.ava}"
app:avaPlaceHolder="@{@drawable/image_default}" />
<TextView
android:id="@+id/databinding_recyclerview_item_textview"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_centerVertical="true"
android:layout_marginStart="15dp"
android:layout_toEndOf="@+id/databinding_recyclerview_item_imageview"
android:gravity="center|left"
android:onClick="@{RecyclerViewItemClick::textViewClick}"
android:text="@{databinding.title}"
tools:ignore="RtlHardcoded"
tools:text="张三" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#88CCCCCC">
</View>
</LinearLayout>
</layout>
加载图片
public class ImageViewUtils {
/**
* DataBinding加载图片
*/
@BindingAdapter({"app:avaImageUrl", "app:avaPlaceHolder", "app:avaError"})
public static void loadImageView(ImageView imageView, String imageUri, Drawable placeHolder, Drawable error) {
if (null == imageView) return;
Glide.with(imageView.getContext())
.load(imageUri)
.error(error)
.placeholder(placeHolder)
.into(imageView);
Log.d("ImageViewUtils", "图片路径 imageUri----:" + imageUri);
}
}