Android jetpack Databinding 双向绑定

Posted 安果移不动

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android jetpack Databinding 双向绑定相关的知识,希望对你有一定的参考价值。

1、比较麻烦的方式

新建user类

package com.anguomob.jecpack.bean

data class User(var userName:String)

新建viewModel 继承自BaseObservable

package com.anguomob.jecpack.viewmodel

import android.util.Log
import androidx.databinding.BaseObservable
import androidx.databinding.Bindable
import androidx.databinding.library.baseAdapters.BR
import com.anguomob.jecpack.bean.User

class UserViewModel : BaseObservable() 
    private lateinit var user: User
    private val TAG = "UserViewModel"

    init 
        this.user = User("Jack")
    

    //数据发生变化 就会更新
    @Bindable
    fun getUserName(): String 
        return user.userName
    

    fun setUserName(userName: String) 
        if (userName != null && userName != "" && userName != user.userName) 
            user.userName = userName;
            Log.e(
                TAG, "setUserNmae: $userName"
            )
            notifyPropertyChanged(BR.userName)

        

    


布局

<?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="userViewModel"
            type="com.anguomob.jecpack.viewmodel.UserViewModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".activity.TwoWayBindingActivity">

        <EditText
            android:id="@+id/editTextTextPersonName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="@=userViewModel.userName"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

代码

package com.anguomob.jecpack.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import com.anguomob.jecpack.R
import com.anguomob.jecpack.databinding.ActivityDatabindingAdapterBinding
import com.anguomob.jecpack.databinding.ActivityTwoWayBindingBinding
import com.anguomob.jecpack.viewmodel.UserViewModel

class TwoWayBindingActivity : AppCompatActivity() 
    private lateinit var binding: ActivityTwoWayBindingBinding
    override fun onCreate(savedInstanceState: Bundle?) 
        super.onCreate(savedInstanceState)
        binding= DataBindingUtil.setContentView(this, R.layout.activity_two_way_binding)
        binding.userViewModel= UserViewModel()
    

当输入框输入文字后。也会打印出来对应tag

所以双向绑定完成

2、简单方式

 

package com.anguomob.jecpack.viewmodel

import android.util.Log
import androidx.databinding.BaseObservable
import androidx.databinding.Bindable
import androidx.databinding.ObservableField
import androidx.databinding.library.baseAdapters.BR
import com.anguomob.jecpack.bean.User

class UserViewModel2 
    private lateinit var userObservableField: ObservableField<User>
    private  val TAG = "UserViewModel2"
    init 
        userObservableField = ObservableField();
        userObservableField.set(User("jack"))
    

    fun getUserName(): String? 
        return userObservableField.get()?.userName;
    

    fun setUserName(userName: String) 
        Log.e(
            TAG, "setUserNmae: $userName"
        )
        userObservableField.get()?.userName = userName;
    

布局

<?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="userViewModel"
            type="com.anguomob.jecpack.viewmodel.UserViewModel2" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".activity.TwoWayBindingActivity">

        <EditText
            android:id="@+id/editTextTextPersonName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="@=userViewModel.userName"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

页面

package com.anguomob.jecpack.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import com.anguomob.jecpack.R
import com.anguomob.jecpack.databinding.ActivityDatabindingAdapterBinding
import com.anguomob.jecpack.databinding.ActivityTwoWayBindingBinding
import com.anguomob.jecpack.viewmodel.UserViewModel
import com.anguomob.jecpack.viewmodel.UserViewModel2

class TwoWayBindingActivity : AppCompatActivity() 
    private lateinit var binding: ActivityTwoWayBindingBinding
    override fun onCreate(savedInstanceState: Bundle?) 
        super.onCreate(savedInstanceState)
        binding= DataBindingUtil.setContentView(this, R.layout.activity_two_way_binding)
        binding.userViewModel= UserViewModel2()
    

更为简单。

以上是关于Android jetpack Databinding 双向绑定的主要内容,如果未能解决你的问题,请参考以下文章

Android Jetpack架构组件带你了解Android Jetpack

Android Jetpack简介

Android Jetpack架构组件——什么是Jetpack?

Android Jetpack架构组件——什么是Jetpack?

《Android Jetpack从入门到精通+高级Jetpack强化实战》,最新Jetpack系列开发笔记开源

Android高级Jetpack架构组件+Jetpack compose强化实战