Android DataBinding手把手入门教程
作者:€€-飞翔 -~£ 时间:2023-04-29 18:51:24
1、在build.gradle(Module)里引入依赖,然后重构(sync Now):
android {
......
dataBinding{
enabled true
}
}
2、找到想要改为dataBinding视图的页面,alt+enter弹出如下:
并选择Convert to data binding layout自动转换。
转换之后效果应和下图类似:
可以看到页面出现了新的Layout和data的空标签。(data 就是用来存放数据的)
3、接下来到对应的Activity里,声明全局变量为视图去掉下划线改大写+binding:
如视图为activity_main,则对应声明的databinding类型变量就是ActivityMainBinding类型,如下:
此时在Oncreate()里输入如下代码:
//binding的对象实例化,第二个参数为对应的视图id
//binding=DataBindingUtil.setContentView(this,视图id);
binding= DataBindingUtil.setContentView(this,R.layout.activity_data_banding_test);
//你的ViewModel实例化,ViewModel部分就不再详细讲了,也可以用其他类型数据
viewModelWithDataBanding=new ViewModelProvider(this,new ViewModelProvider.NewInstanceFactory()).get(ViewModelWithDataBanding.class);
//将你准备好的数据,可以是ViewModel,通过binding.setData();绑定到视图
binding.setData(viewModelWithDataBanding);
//设置绑定的生命周期,这句很必要!!!
binding.setLifecycleOwner(this);
4、再次返回你的视图,绑定你的数据如下:
<?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="data"
type="com.example.mytests.viewModels.ViewModelWithDataBanding"
/>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.DataBandingTest">
<TextView
android:id="@+id/textView2_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{String.valueOf(data.number)}"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.257" />
<Button
android:id="@+id/button2_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="@{()->data.add()}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2_1" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
首先,声明变量<variable name="你自定义变量名,data" type="你之前.setData()的数据的数据类型"/>
然后,绑定变量如:
android:text="@{你自定义变量名.变量下的数据}";
绑定方法是注意:如:
android:onClick="@{()->data.add()}",需在方法前添加()->表示这是方法。
5、在视图对应的activity中可通过你声明的绑定对象来访问对应的视图控件,如:
需要设置id为button12的点击事件,可直接用绑定对象.button12.setOnclickListener(this);来操作,减少了代码的重复性,简洁美观。
操作控件案例截图:
来源:https://blog.csdn.net/qq_50909707/article/details/120654572
标签:Android,DataBinding
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android解析XML文件升级APK的方法
2022-06-05 20:47:26
Android实现本地图片选择及预览缩放效果
2023-09-27 14:07:28
![](https://img.aspxhome.com/file/2023/3/93313_0s.gif)
android的got表HOOK实现代码
2023-11-18 17:04:51
JDK源码分析之String、StringBuilder和StringBuffer
2022-01-23 00:10:20
![](https://img.aspxhome.com/file/2023/0/68160_0s.jpg)
对Jpa中Entity关系映射中mappedBy的全面理解
2023-07-25 03:48:39
Android编程之重力感应用法分析
2021-09-04 19:49:35
ReentrantLock源码详解--条件锁
2023-01-01 15:36:22
![](https://img.aspxhome.com/file/2023/8/69108_0s.png)
Android开发RecyclerView实现折线图效果
2022-08-27 12:59:34
![](https://img.aspxhome.com/file/2023/5/108325_0s.jpg)
Spring Boot2中如何优雅地个性化定制Jackson实现示例
2021-09-27 12:21:15
swing登录注册界面设计
2023-11-24 17:27:21
![](https://img.aspxhome.com/file/2023/5/62825_0s.jpg)
C# 游戏外挂实现核心代码
2021-12-28 14:47:40
Cocos2d-x入门教程(详细的实例和讲解)
2023-07-31 17:18:00
解析Android开发优化之:对界面UI的优化详解(一)
2023-05-23 17:45:10
浅入浅出的讲解Spring循环依赖问题
2023-11-03 07:16:11
![](https://img.aspxhome.com/file/2023/5/79285_0s.png)
java调用微信现金红包接口的心得与体会总结
2022-12-22 19:55:12
分析设计模式之模板方法Java实现
2022-01-20 21:02:22
![](https://img.aspxhome.com/file/2023/9/101969_0s.jpg)
SpringBoot打成war包在tomcat或wildfly下运行的方法
2023-11-23 08:20:56
基于WPF实现代码查看器控件
2022-10-06 03:32:11
![](https://img.aspxhome.com/file/2023/3/92873_0s.gif)
Android中LayoutInflater.inflater()的正确打开方式
2022-04-12 16:25:57
iOS获取AppIcon and LaunchImage's name(app图标和启动图片名字)
2022-01-11 02:39:14
![](https://img.aspxhome.com/file/2023/7/78917_0s.png)