Android數(shù)據(jù)雙向綁定原理實(shí)現(xiàn)和應(yīng)用場(chǎng)景
安卓的數(shù)據(jù)雙向綁定類似Vue這種前端框架,只要修改模型的數(shù)據(jù),頁(yè)面上顯示的數(shù)據(jù)也會(huì)跟著變化,不需要取出控件來賦值。
一、使用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());
}
}
接下來就可以使用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í)開發(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 中對(duì)JSON數(shù)據(jù)解析實(shí)例代碼
這篇文章主要介紹了Android 中對(duì)JSON數(shù)據(jù)解析實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03
Android音頻編輯之音頻轉(zhuǎn)換PCM與WAV
這篇文章主要為大家詳細(xì)介紹了Android音頻編輯之音頻轉(zhuǎn)換PCM與WAV,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
Android實(shí)戰(zhàn)教程第十篇仿騰訊手機(jī)助手小火箭發(fā)射效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)戰(zhàn)教程第十篇仿騰訊手機(jī)助手小火箭發(fā)射效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android開發(fā)筆記之:在ImageView上繪制圓環(huán)的實(shí)現(xiàn)方法
本篇文章是對(duì)Android中在ImageView上繪制圓環(huán)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android Studio真機(jī)無線連接USB設(shè)備調(diào)試運(yùn)行詳解流程
你在Android Studio寫app時(shí)是否也有想過如果可以不用數(shù)據(jù)線連接手機(jī)調(diào)試運(yùn)行就好了?如果需要取出數(shù)據(jù)線插接的話我肯定是嫌麻煩的,但是模擬器有時(shí)候需要測(cè)試一些需要硬件支持的功能時(shí)又不管用,所以最好的測(cè)試還是在真機(jī)上,本篇教你扔掉數(shù)據(jù)線來無線調(diào)試2021-11-11
RecyclerView實(shí)現(xiàn)水波紋點(diǎn)擊效果
這篇文章主要為大家詳細(xì)介紹了RecyclerView實(shí)現(xiàn)水波紋點(diǎn)擊效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01

