Android數(shù)據(jù)雙向綁定原理實(shí)現(xiàn)和應(yīng)用場(chǎng)景
安卓的數(shù)據(jù)雙向綁定類似Vue這種前端框架,只要修改模型的數(shù)據(jù),頁(yè)面上顯示的數(shù)據(jù)也會(huì)跟著變化,不需要取出控件來(lái)賦值。
一、使用databinding類
修改配置文件build.gradle,增加配置項(xiàng)
android {
...
buildFeatures {
viewBinding true
}
}
修改Activity類獲取binding屬性
public class MainActivity extends AppCompatActivity { ActivityMainBinding binding; private ProgressDialog pg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = ActivityMainBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); } }
接下來(lái)就可以使用binding獲取頁(yè)面的元素了,頁(yè)面的控件就是binding的一個(gè)屬性,不再需要使用findViewById方法取得控件。
比如:
binding.imageview
binding.btn
二、雙向綁定
1、增加綁定配置
修改配置文件build.gradle,增加兩個(gè)配置項(xiàng)
android {
...
defaultConfig {
...
dataBinding {
enabled true
}
}
...
buildFeatures {
viewBinding true
}
}
2、修改布局文件(activity_main.xml),增加一層layout
格式如下:
根節(jié)點(diǎn)是
節(jié)點(diǎn)聲明了需要綁定的變量
@{user.text}:在頁(yè)面上顯示模型屬性
@={user.text}:雙向綁定,修改控件的值后,同步修改模型屬性值
<?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.nbmt.cash.BindingEntity" /> </data> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp" android:id="@+id/textView" android:text="@{user.text}" android:background="@color/purple_200" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/edit_text" android:layout_width="wrap_content" android:layout_marginTop="20dp" android:textSize="30sp" android:layout_height="wrap_content" android:text="@={user.text}" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView" /> </LinearLayout> </layout>
3、在Activity中使用
1)創(chuàng)建模型對(duì)象,必須繼承基類androidx.databinding.BaseObservable
- @Bindable:聲明該屬性可以用于綁定
- 修改setXX方法,調(diào)傭
notifyPropertyChanged(BR.text)
發(fā)送修改通知;也可以調(diào)用notifyChange()
通知所有屬性
import androidx.databinding.BaseObservable; import androidx.databinding.Bindable; public class BindingEntity extends BaseObservable { private String text; public BindingEntity(String text) { this.text = text; } @Bindable public String getText() { return text; } public void setText(String text) { this.text = text; notifyPropertyChanged(BR.text); } }
2)修改Activity,使用binding對(duì)象
- ActivityMainBinding是框架自動(dòng)生成的,和MainActivity對(duì)應(yīng)
- 使用
DataBindingUtil.setContentView(this, R.layout.activity_main)
獲取綁定對(duì)象 - 去掉
setContentView(R.layout.activity_main)
public class MainActivity extends AppCompatActivity { private ActivityMainBinding binding; private BindingEntity entity; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = DataBindingUtil.setContentView(this, R.layout.activity_main); //setContentView(R.layout.activity_main); entity = new BindingEntity("我是測(cè)試數(shù)據(jù)"); binding.setUser(entity); } }
后續(xù)只要修改entity的屬性值,頁(yè)面控件就會(huì)自動(dòng)跟著變化
到此這篇關(guān)于Android數(shù)據(jù)雙向綁定原理實(shí)現(xiàn)和應(yīng)用場(chǎng)景的文章就介紹到這了,更多相關(guān)Android數(shù)據(jù)雙向綁定內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android DataBinding單向數(shù)據(jù)綁定深入探究
- 淺析Android企業(yè)級(jí)開(kāi)發(fā)數(shù)據(jù)綁定技術(shù)
- Android Studio綁定下拉框數(shù)據(jù)詳解
- 詳解Android的MVVM框架 - 數(shù)據(jù)綁定
- Android Data Binding數(shù)據(jù)綁定詳解
- Android RecyclerView 數(shù)據(jù)綁定實(shí)例代碼
- Android ListView數(shù)據(jù)綁定顯示的三種解決方法
- Android中 自定義數(shù)據(jù)綁定適配器BaseAdapter的方法
相關(guān)文章
Android apk無(wú)法安裝及閃退問(wèn)題解決辦法
這篇文章主要介紹了Android apk無(wú)法安裝及閃退問(wèn)題的相關(guān)資料,這里對(duì)閃退問(wèn)題進(jìn)行詳解及解決步驟的詳細(xì)介紹,需要的朋友可以參考下2017-07-07基于Android中獲取資源的id和url方法總結(jié)
下面小編就為大家分享一篇基于Android中獲取資源的id和url方法總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02Ubuntu中為Android實(shí)現(xiàn)Application Frameworks層增加硬件訪問(wèn)服務(wù)
本文主要介紹Android實(shí)現(xiàn) Application Frameworks層增加硬件訪問(wèn)服務(wù),這里對(duì)實(shí)現(xiàn)增加硬件訪問(wèn)服務(wù)的功能做出了詳細(xì)的工作流程,并提供示例代碼,有需要的小伙伴參考下2016-08-08Android 關(guān)于ExpandableListView去掉里頭分割線的方法
下面小編就為大家?guī)?lái)一篇Android 關(guān)于ExpandableListView去掉里頭分割線的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12Android 藍(lán)牙開(kāi)發(fā)實(shí)例解析
本文主要介紹Android 藍(lán)牙開(kāi)發(fā),這里提供實(shí)例代碼和詳細(xì)解析實(shí)現(xiàn)方法,對(duì)開(kāi)發(fā)Android藍(lán)牙開(kāi)發(fā)的朋友提供簡(jiǎn)單示例,有需要的朋友可以參考下2016-08-08Android靜默安裝實(shí)現(xiàn)方案 仿360手機(jī)助手秒裝和智能安裝功能
這篇文章主要介紹了Android靜默安裝實(shí)現(xiàn)方案,仿360手機(jī)助手秒裝和智能安裝功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Android自定義Dialog實(shí)現(xiàn)加載對(duì)話框效果
這篇文章將介紹如何定制當(dāng)今主流的對(duì)話框,通過(guò)自定義dialog實(shí)現(xiàn)加載對(duì)話框效果,具體實(shí)現(xiàn)代碼大家通過(guò)本文學(xué)習(xí)吧2018-05-05Android模擬美團(tuán)客戶端進(jìn)度提示框
這篇文章主要為大家詳細(xì)介紹了Android模擬美團(tuán)客戶端進(jìn)度提示框的實(shí)現(xiàn)過(guò)程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-09-09