当前位置:网站首页>* 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
}
}
边栏推荐
- 初识企业级平台
- SQL statement when the query condition is blank, all data will be queried by default. If it is not blank, the query will be performed according to the condition
- 在同花顺上面开股票账户好不好,安不安全?
- VIM common commands
- Say no to credit card fraud! 100 lines of code to realize simplified real-time fraud detection
- Hwang
- Why OKR needs to be challenging
- [c language] output the average score and the student data below or equal to the average score with the structure
- NFT platform development NFT mall source code NFT mall development chain game development
- On the problem that the while loop condition in keil does not hold but cannot jump out
猜你喜欢

H. 264 concept
![Winter vacation daily question 2022 [week1 not finished]](/img/56/a5ca98ddd128bcceb8e24f0edfeac6.jpg)
Winter vacation daily question 2022 [week1 not finished]

SISO decoder for a general (n,n-1) SPC code(补充章节3)

SISO Decoder for SPC (补充章节1)

async导致函数结果出乎意料,改变原来代码的意图;await is only valid in async functions and the top level bodies of modules
![[c language] compress strings and add markup characters](/img/b7/f7918f3ee0c409faffc70addd5ee65.png)
[c language] compress strings and add markup characters

【无标题】

“LSTM之父”新作:一种新方法,迈向自我修正的神经网络

TR-069协议介绍

NR LDPC 打孔-punctured
随机推荐
[Golang]力扣Leetcode - 349. 两个数组的交集(哈希表)
新项目 搭建环境方法
Network Security Threat Intelligence System
“LSTM之父”新作:一种新方法,迈向自我修正的神经网络
Experiment 3: design and verify all operations represented by linear table sequence on the computer
密码学概述
General terms in security field
Feign 共享登录信息进行请求
Secret comment-----
Radiogroup dynamically add RadioButton
ISCSI详解(四)——ISCSI服务端配置实战
TR-069 protocol introduction
10 ways to reset any user password
Jsfinder, wafw00f installation, nmap configuration (msvcr120.dll file is missing)
互联网_业务分析概述
labelme进行图片数据标注
论催收系统的管理子系统选型设计
[C语言]用结构体按分数高低降序输出学生的姓名和分数
Spring 2021 daily question [week7 not finished]
async导致函数结果出乎意料,改变原来代码的意图;await is only valid in async functions and the top level bodies of modules