当前位置:网站首页>* Jetpack 笔记 使用DataBinding
* Jetpack 笔记 使用DataBinding
2022-06-11 18:14:00 【Xia_燚】
昨天学习了LifeCycle ViewModel LiveData,今天学习一下DataBinding
使用DataBinding 的意义
让布局文件承担了原属于页面的工作,使页面布局耦合度进一步降低
DataBinding的优势
不在需要findViewById,项目更加简洁,可读性更高。
布局文件可以包含简单的业务逻辑。

使用DataBinding
在 build.gradle 文件中配置 在defaultConfig方法下
dataBinding{
enabled = true
}
DataBinding 意义与应用
xml 文件中按住alt+回车 生成data,Idol 是实体类,idol可以理解为对象
<data>
<variable
name="idol"
type="com.example.databing.Idol" />
<variable
name="eventHandle"
type="com.example.databing.EventHandleListener" />
<import type="com.example.databing.StartUtils" />
</data>
TextView 展示
<TextView
android:id="@+id/tv_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@{StartUtils.getStar(idol.start)}"
android:textColor="#333"
android:textSize="15sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_one"
tools:text="评分" />
Activity 相关代码
ActivityDataBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_data);
Idol idol = new Idol("洛洛亚索隆",2);
binding.setIdol(idol);
加载网络/本地图片
//java 代码
public class ImageViewBindingAdapter {
@BindingAdapter("image")
public static void setImage(ImageView imageView, String url){
if(!TextUtils.isEmpty(url)){
Picasso.get()
.load(url)
.placeholder(R.drawable.ic_launcher_background)
.into(imageView);
}else{
imageView.setBackgroundColor(Color.GRAY);
}
}
}
xml 文件 type 指的是 name 对象的类型
<variable
name="networkImage"
type="String" />
ImageView
这个image 这个属性 一定要是 BindingAdapter 带注解的这个东西@BindingAdapter(“image”)
app:image=“@{networkImage}”
使用过RecycleView 绑定
Xml 中需要注意的 itemImage 属性为
ImageViewBindingAdapter 中 @BindingAdapter(“itemImage”)
ImageViewBindingAdapter 代码如下
public class ImageViewBindingAdapter {
@BindingAdapter("itemImage")
public static void setImage(ImageView imageView, String url){
if(!TextUtils.isEmpty(url)){
Picasso.get()
.load(url)
.placeholder(R.drawable.ic_launcher_background)
.into(imageView);
}else{
imageView.setBackgroundColor(Color.GRAY);
}
}
}
主要的绑定关系没有在activity中,而是在Item 中 ItemBinding
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
List<Idol> idols;
public RecyclerViewAdapter(List<Idol> idols) {
this.idols = idols;
}
@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) {
Idol idol = idols.get(position);
holder.itemBinding.setIdol(idol);
}
@Override
public int getItemCount() {
return idols.size();
}
static class MyViewHolder extends RecyclerView.ViewHolder {
private ItemBinding itemBinding;
public MyViewHolder(@NonNull ItemBinding itemBinding) {
super(itemBinding.getRoot());
this.itemBinding = itemBinding;
}
}
}
Kotlin RecycleView使用 DataBinding 的Adapter
自己练习的时候写的代码,让自己慢慢适应Kotlin
class StudentRecyclerViewAdapter(var items: List<Student>) :
RecyclerView.Adapter<StudentRecyclerViewAdapter.ViewHolder>() {
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): StudentRecyclerViewAdapter.ViewHolder {
val itemBinding: ItemBinding = DataBindingUtil.inflate(
LayoutInflater.from(parent.context),
R.layout.item,
parent,
false
)
return ViewHolder(itemBinding)
}
override fun getItemCount(): Int = items.size
override fun onBindViewHolder(holder: StudentRecyclerViewAdapter.ViewHolder, position: Int) {
holder.itemDataBindX.student = items[position]
}
class ViewHolder(itemBinding: ItemBinding) : RecyclerView.ViewHolder(itemBinding.root) {
var itemDataBindX:ItemBinding = itemBinding
}
fun setStudents(students: List<Student>) {
items = students
}
}
边栏推荐
- 论高可用架构
- Codeworks round 479 (Div. 3) [done]
- Hwang
- Some problems of DC-DC bootstrap capacitor
- 最长严格递增子序列
- Say no to credit card fraud! 100 lines of code to realize simplified real-time fraud detection
- LeetCode_ Prefix tree_ Medium_ 208. implement trie (prefix tree)
- Cryptology Summary
- Getting started with CTF
- labelme进行图片数据标注
猜你喜欢
随机推荐
[c language] shift elements after sorting elements of an array
力扣32题最长有效括号
01.电信_领域业务经验
H.264概念
Système d'information sur les menaces à la sécurité des réseaux
初识企业级平台
神经网络与深度学习-2- 机器学习简单示例-PyTorch
Summary of common mysql/redis interview questions
System learning typescript (V) - joint type
Codeworks round 481 (Div. 3) [done]
The HashSet collection stores student objects and traverses
[golang] leetcode - 292 Nim games (Mathematics)
Spring 2021 daily question [end of week4]
[FAQs for novices on the road] about project management
Why OKR needs to be challenging
论高可用架构
[document operation] of ACM
【无标题】
力扣39题组合总和
Experiment 2: write a program and verify that the linear table sequence represents all operations




![[Golang]力扣Leetcode - 292. Nim 游戏(数学)](/img/82/54c3f6be9d08687b42cba0487380f0.png)



