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

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

posted @ 2019-06-14 10:03  jxiaow  阅读(1194)  评论(3编辑  收藏  举报