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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM