Android DataBinding 双向数据绑定、事件绑定、使用类方法


一、Android DataBinding 基础使用
二、Android DataBinding单向绑定
三、Android DataBinding 双向数据绑定、事件绑定、使用类方法
四、Android DataBinding 运算符、BindingAdapter、 BindingConversion


双向数据绑定

双向绑定的意思即为当数据改变时同时使视图刷新,而视图改变时也可以同时改变数据

看以下例子,当 EditText的输入内容改变时,会同时同步到变量 user,绑定变量的方式比单向绑定多了一个等号:android:text="@={user.name}"

<?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="userInfo" type="com.github.ixiaow.sample.model.ObservableUser"/>    data>    <androidx.constraintlayout.widget.ConstraintLayout            android:layout_width="match_parent"            android:layout_height="match_parent"            tools:context=".MainActivity">        <EditText                app:layout_constraintVertical_chainStyle="spread"                android:id="@+id/mUserName"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="@={userInfo.name, default=`name`}"                app:layout_constraintBottom_toBottomOf="parent"                app:layout_constraintLeft_toLeftOf="parent"                app:layout_constraintRight_toRightOf="parent"                app:layout_constraintTop_toTopOf="parent"/>        <EditText                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="@={userInfo.password, default=`1234`}"                app:layout_constraintTop_toBottomOf="@id/mUserName"                app:layout_constraintBottom_toBottomOf="parent"                app:layout_constraintLeft_toLeftOf="parent"                app:layout_constraintRight_toRightOf="parent"/>    androidx.constraintlayout.widget.ConstraintLayout>layout>
class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        val dataBinding: ActivityMainBinding =            DataBindingUtil.setContentView(this, R.layout.activity_main)        val observableUser = ObservableUser()        observableUser.name.set("我是name")        observableUser.password.set("我是password")        dataBinding.userInfo = observableUser    }}

事件绑定

严格意义上来说,事件绑定也是一种变量绑定,只不过设置的变量是回调接口而已。

事件绑定可用于以下多种回调事件:

  • android:onClick
  • android:onLongClick
  • android:afterTextChanged
  • android:onTextChanged

新建一个 UserPresenter 类来声明 onClick()afterTextChanged() 事件相应的回调方法

class UserPresenter {    fun onUserNameClick(user: ObservableUser) {    }    fun afterTextChanged(s: Editable) {    }    fun saveUser(view: View, user: ObservableUser){    }}
<?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>        <import type="com.github.ixiaow.sample.UserPresenter"/>        <variable name="presenter" type="UserPresenter"/>        <variable name="userInfo" type="com.github.ixiaow.sample.model.ObservableUser"/>    data>    <androidx.constraintlayout.widget.ConstraintLayout            android:layout_width="match_parent"            android:layout_height="match_parent"            tools:context=".MainActivity">        <EditText                app:layout_constraintVertical_chainStyle="spread"                android:id="@+id/mUserName"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:onClick="@{()-> presenter.onUserNameClick(userInfo)}"                android:text="@={userInfo.name, default=`name`}"                app:layout_constraintBottom_toBottomOf="parent"                app:layout_constraintLeft_toLeftOf="parent"                app:layout_constraintRight_toRightOf="parent"                app:layout_constraintTop_toTopOf="parent"/>        <EditText                android:id="@+id/mPassword"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:afterTextChanged="@{presenter::afterTextChanged}"                android:text="@={userInfo.password, default=`1234`}"                app:layout_constraintTop_toBottomOf="@id/mUserName"                app:layout_constraintBottom_toBottomOf="parent"                app:layout_constraintLeft_toLeftOf="parent"                app:layout_constraintRight_toRightOf="parent"/>        <EditText                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:onClick="@{(theView)->presenter.saveUser(theView, userInfo)}"                android:text="@={userInfo.password, default=`1234`}"                app:layout_constraintTop_toBottomOf="@id/mPassword"                app:layout_constraintBottom_toBottomOf="parent"                app:layout_constraintLeft_toLeftOf="parent"                app:layout_constraintRight_toRightOf="parent"/>    androidx.constraintlayout.widget.ConstraintLayout>layout>

方法引用的方式与调用函数的方式类似,

  1. 可以选择保持事件回调方法的签名一致:@{presenter.afterTextChanged},此时方法名可以不一样,但方法参数和返回值必须和原始的回调函数保持一致。
  2. 可以引用不遵循默认签名的函数:@{()->presenter.onUserNameClick(userInfo)},这里用到了 Lambda 表达式,这样就可以不遵循默认的方法签名,将userInfo对象直接传回点击方法中。此外,也可以使用方法引用 :: 的形式来进行事件绑定

使用类方法

首先定义一个静态方法

object StringUtils {   fun toUpperCase( str:String):String {        return str.toUpperCase();   }}

在 data 标签中导入该工具类

 <import type="com.github.ixiaow.sample.StringUtils" />

然后就可以像对待一般的函数一样来调用了

<TextView   android:layout_width="match_parent"   android:layout_height="wrap_content"   android:onClick="@{()->userPresenter.onUserNameClick(userInfo)}"   android:text="@{StringUtils.toUpperCase(userInfo.name)}" />

本地址:https://blog.csdn.net/xiaowu_zhu/article/details/91951474

更多相关文章

  1. Android(安卓)Studio编译一次源码versionCode自增
  2. Android培训班(14)
  3. ThreadLocal原理
  4. Android中写文本文件的方法
  5. stagefright架构
  6. Android(安卓)dialog 去除虚拟按键的解决方法
  7. Android中的WebView控件用法
  8. android系统关机源码(9.0)流程分析
  9. android标题栏进度圈使用方法

随机推荐

  1. Android------播放音乐的工具类
  2. 浅谈Android中的Handler
  3. 创建自定义视图在Android矩阵效果画布教
  4. Android中Activity、Service和线程之间的
  5. 基于 Android NDK 的学习之旅-----数据传
  6. Android入门教程(二十三)------之Gallery
  7. 基于Android的Word文档阅读器
  8. Android中父View和子view的点击事件的执
  9. Android 网络通信框架Volley的简单使用
  10. Kotlin从入门到沉迷