美文网首页
databinding

databinding

作者: 闫鹏飞写字的地方 | 来源:发表于2022-06-21 09:26 被阅读0次

    官方地址:https://developer.android.google.cn/topic/libraries/data-binding
    https://developer.android.google.cn/jetpack/androidx/releases/databinding?hl=zh_cn

    B站地址:https://www.bilibili.com/video/BV1fY4y1i7BG?p=10&vd_source=c9e619eb6c2ba53337eccc49eb025732

    android {
        ...
        buildFeatures {
            dataBinding true
        }
    }
    

    单向数据绑定

    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            var dataBinding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
            dataBinding.user = User("张三")
            dataBinding.lifecycleOwner = this
        }
    }
    
    <?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="user"
                type="com.wy.databinding.User" />
        </data>
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@{user.name}"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    </layout>
    
    data class User(var name:String)
    

    数据双向绑定

    class User(name:String){
         val _name =ObservableField<String>()
        init {
            _name.set(name)
        }
    }
    
    class MainActivity : AppCompatActivity() {
        private val user = User("张三")
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            val dataBinding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
            dataBinding.user = user
            dataBinding.lifecycleOwner = this
            dataBinding.btnChangeName.setOnClickListener(View.OnClickListener {
                user._name.set("李四")
            })
        }
    }
    
    <?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="user"
                type="com.wy.databinding.User" />
        </data>
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity">
    
            <Button
                android:id="@+id/btnChangeName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toTopOf="parent"
                android:text="更改名字"/>
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@{user._name}"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    </layout>
    

    自定义属性-BindingAdapter

    plugins {
        id 'com.android.application'
        id 'kotlin-android'
        id 'kotlin-kapt'
    }
    
    class Test {
        companion object{
            @BindingAdapter(value =["imageUrl"], requireAll = false)
            @JvmStatic
            fun test(imageView: ImageView, imageUrl:String) {
                Glide.with(imageView.context).load(imageUrl).into(imageView)
            }
        }
    }
    
    class BindingAdapterActivity : AppCompatActivity() {
        private val user = User("张三", "https://img0.baidu.com/it/u=2869855171,615441262&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500")
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_binding_adapter)
            val dataBinding = DataBindingUtil.setContentView<ActivityBindingAdapterBinding>(this, R.layout.activity_binding_adapter)
            dataBinding.user = user
            dataBinding.lifecycleOwner = this
        }
    }
    
    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <data>
            <variable
                name="user"
                type="com.wy.databinding.User" />
        </data>
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".bindingadapter.BindingAdapterActivity">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:imageUrl="@{user._imageUrl}"/>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </layout>
    

    相关文章

      网友评论

          本文标题:databinding

          本文链接:https://www.haomeiwen.com/subject/iesxvrtx.html