Android實(shí)現(xiàn)MVVM架構(gòu)數(shù)據(jù)刷新詳解流程
效果圖

示例結(jié)構(gòu)圖

代碼解析
導(dǎo)入dataBinding
dataBinding{
enabled = true
}
實(shí)體類
繼承BaseObservable
public class Sensor extends BaseObservable
為字段添加@Bindable
@Bindable
public String getTmpValue() {
return tmpValue;
}
顯示圖片
圖片添加@BindingAdapter
@BindingAdapter( "tmpImage" )
示例采用本地圖片,沒有采用網(wǎng)絡(luò)圖片
@BindingAdapter( "tmpImage" )
public static void setTmpImage(ImageView view, int tmpImage) {
view.setImageDrawable( view.getContext().getResources().getDrawable( tmpImage ) );
}
為圖片路徑綁定字段
@Bindable
public int getTmpImage() {
return tmpImage;
}
綁定實(shí)體類
<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="viewmodel"
type="com.franzliszt.refreshdata.viewmodel.ViewModel" />
</data>
<layout/>
根據(jù)設(shè)置@BindingAdapter( “tmpImage” )設(shè)置里的內(nèi)容,設(shè)置屬性
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
tmpImage="@{viewmodel.sensor.tmpImage}"
android:scaleType="fitCenter"/>
實(shí)體類全部代碼
public class Sensor extends BaseObservable {
private String tmpValue;
private String humValue;
private String lightValue;
private String humanValue;
private String smokeValue;
private String fireValue;
private int tmpImage;
private int humImage;
private int lightImage;
private int humanImage;
private int smokeImage;
private int fireImage;
public Sensor(){
}
public Sensor(int tmpImage,int humImage,int lightImage,int humanImage,int smokeImage,int fireImage){
this.tmpImage = tmpImage;
this.humImage = humImage;
this.lightImage = lightImage;
this.humanImage = humanImage;
this.smokeImage = smokeImage;
this.fireImage = fireImage;
}
@Bindable
public String getTmpValue() {
return tmpValue;
}
public void setTmpValue(String tmpValue) {
this.tmpValue = tmpValue;
notifyPropertyChanged( BR.tmpValue );
}
@Bindable
public String getHumValue() {
return humValue;
}
public void setHumValue(String humValue) {
this.humValue = humValue;
notifyPropertyChanged( BR.humValue );
}
@Bindable
public String getLightValue() {
return lightValue;
}
public void setLightValue(String lightValue) {
this.lightValue = lightValue;
notifyPropertyChanged( BR.lightValue );
}
@Bindable
public String getHumanValue() {
return humanValue;
}
public void setHumanValue(String humanValue) {
this.humanValue = humanValue;
notifyPropertyChanged( BR.humanValue );
}
@Bindable
public String getSmokeValue() {
return smokeValue;
}
public void setSmokeValue(String smokeValue) {
this.smokeValue = smokeValue;
notifyPropertyChanged( BR.smokeValue );
}
@Bindable
public String getFireValue() {
return fireValue;
}
public void setFireValue(String fireValue) {
this.fireValue = fireValue;
notifyPropertyChanged( BR.fireValue );
}
@Bindable
public int getTmpImage() {
return tmpImage;
}
@BindingAdapter( "tmpImage" )
public static void setTmpImage(ImageView view, int tmpImage) {
view.setImageDrawable( view.getContext().getResources().getDrawable( tmpImage ) );
}
@Bindable
public int getLightImage() {
return lightImage;
}
@BindingAdapter( "lightImage" )
public static void setLightImage(ImageView view,int lightImage) {
view.setImageDrawable( view.getContext().getResources().getDrawable( lightImage ) );
}
@Bindable
public int getHumanImage() {
return humanImage;
}
@BindingAdapter( "humanImage" )
public static void setHumanImage(ImageView view,int humanImage) {
view.setImageDrawable( view.getContext().getResources().getDrawable( humanImage ) );
}
@Bindable
public int getSmokeImage() {
return smokeImage;
}
@BindingAdapter( "smokeImage" )
public static void setSmokeImage(ImageView view,int smokeImage) {
view.setImageDrawable( view.getContext().getResources().getDrawable( smokeImage ) );
}
@Bindable
public int getFireImage() {
return fireImage;
}
@BindingAdapter( "fireImage" )
public static void setFireImage(ImageView view,int fireImage) {
view.setImageDrawable( view.getContext().getResources().getDrawable( fireImage ) );
}
@Bindable
public int getHumImage() {
return humImage;
}
@BindingAdapter( "humImage" )
public static void setHumImage(ImageView view,int humImage) {
view.setImageDrawable( view.getContext().getResources().getDrawable( humImage ) );
}
}
xml視圖
<?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="viewmodel"
type="com.franzliszt.refreshdata.viewmodel.ViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".view.MainActivity"
android:layout_margin="30dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@drawable/item_bg_style"
android:layout_marginTop="20dp"
android:paddingTop="10dp">
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
tmpImage="@{viewmodel.sensor.tmpImage}"
android:scaleType="fitCenter"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:text="溫度值:"
android:textColor="#ffffff"
android:textSize="20sp"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:text="@{viewmodel.sensor.tmpValue}"
android:textColor="#ffffff"
android:textSize="25sp"
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@drawable/item_bg_style"
android:layout_marginTop="20dp"
android:paddingTop="10dp">
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
humImage="@{viewmodel.sensor.humImage}"
android:scaleType="fitCenter"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:text="濕度值:"
android:textColor="#ffffff"
android:textSize="20sp"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:text="@{viewmodel.sensor.humValue}"
android:textColor="#ffffff"
android:textSize="25sp"
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@drawable/item_bg_style"
android:layout_marginTop="20dp"
android:paddingTop="10dp">
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
lightImage="@{viewmodel.sensor.lightImage}"
android:scaleType="fitCenter"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:text="光照值:"
android:textColor="#ffffff"
android:textSize="20sp"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:text="@{viewmodel.sensor.lightValue}"
android:textColor="#ffffff"
android:textSize="25sp"
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@drawable/item_bg_style"
android:layout_marginTop="20dp"
android:paddingTop="10dp">
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
smokeImage="@{viewmodel.sensor.smokeImage}"
android:scaleType="fitCenter"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:text="煙霧值:"
android:textColor="#ffffff"
android:textSize="20sp"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:text="@{viewmodel.sensor.smokeValue}"
android:textColor="#ffffff"
android:textSize="25sp"
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@drawable/item_bg_style"
android:layout_marginTop="20dp"
android:paddingTop="10dp">
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
fireImage="@{viewmodel.sensor.fireImage}"
android:scaleType="fitCenter"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:text="火焰值:"
android:textColor="#ffffff"
android:textSize="20sp"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:text="@{viewmodel.sensor.fireValue}"
android:textColor="#ffffff"
android:textSize="25sp"
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@drawable/item_bg_style"
android:layout_marginTop="20dp"
android:paddingTop="10dp">
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
humanImage="@{viewmodel.sensor.humanImage}"
android:scaleType="fitCenter"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:text="人體紅外:"
android:textColor="#ffffff"
android:textSize="20sp"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:text="@{viewmodel.sensor.humanValue}"
android:textColor="#ffffff"
android:textSize="25sp"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
</layout>
VM
接收數(shù)據(jù)
調(diào)用Handle類的接口,接收傳感器數(shù)據(jù)(隨機(jī)數(shù)據(jù))
private void InitData(){
handle.setHandleDta( new Handle.HandleData() {
@Override
public void getSensorValue(int[] value) {
new Thread( ()->{
while (true){
try {
Thread.sleep( 5000 );
} catch (InterruptedException e) {
e.printStackTrace();
}
sensor.setTmpValue( value[0]+"℃");
sensor.setHumValue( value[1]+"RH" );
sensor.setLightValue( value[2]+"LUX" );
sensor.setSmokeValue( value[3]+"%" );
sensor.setFireValue( value[4]+"%" );
sensor.setHumanValue( value[5] == 1 ? "有人" : "無人" );
}
} ).start();
}
});
}
發(fā)送數(shù)據(jù)
建立接口,回調(diào)數(shù)據(jù)
public interface HandleData{
void getSensorValue(int[] value);
}
public void setHandleDta(HandleData handleDta){
int[] value = ReturnData();
handleDta.getSensorValue(value);
}
制造數(shù)據(jù)
private void RefreshSensorValue(){
thread = new Thread( ()->{
while (true){
try {
Thread.sleep( 2000 );
} catch (InterruptedException e) {
e.printStackTrace();
}
/*溫度*/
value[0] = RandomRange(35,30);
/*濕度*/
value[1] = RandomRange(80,75);
/*光照值*/
value[2] = RandomRange(120,100);
/*煙霧*/
value[3] = RandomRange(60,50);
/*火焰*/
value[4] = RandomRange(30,25);
/*紅外*/
value[5] = RandomRange(2,0);
Log.d( "TAG",value[5]+"" );
}
} );
thread.start();
}
綁定視圖與數(shù)據(jù)層
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
binding = DataBindingUtil.setContentView( this,R.layout.activity_main );
binding.setViewmodel( new ViewModel() );
}
}
到此這篇關(guān)于Android實(shí)現(xiàn)MVVM架構(gòu)數(shù)據(jù)刷新詳解流程的文章就介紹到這了,更多相關(guān)Android 數(shù)據(jù)刷新內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android EditText 實(shí)現(xiàn)監(jiān)聽實(shí)例
本文主要介紹Android EditText 組件 實(shí)現(xiàn)監(jiān)聽事件,并附有代碼實(shí)例,在Android開發(fā)過程中如果能用到可以參考下2016-07-07
Android控件之SlidingDrawer(滑動(dòng)式抽屜)詳解與實(shí)例分享
這篇文章詳細(xì)介紹了Android控件之SlidingDrawer(滑動(dòng)式抽屜)與實(shí)例,有需要的朋友可以參考一下2013-10-10
Flutter?Animation實(shí)現(xiàn)縮放和滑動(dòng)動(dòng)畫效果
這篇文章主要為大家詳細(xì)介紹了Flutter?Animation實(shí)現(xiàn)縮放和滑動(dòng)動(dòng)畫效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Android自定義View控件實(shí)現(xiàn)多種水波紋漣漪擴(kuò)散效果
這篇文章主要給大家介紹了關(guān)于Android自定義View控件實(shí)現(xiàn)多種水波紋漣漪擴(kuò)散效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-03-03
Android圖片上傳實(shí)現(xiàn)預(yù)覽效果
這篇文章主要介紹了Android圖片上傳實(shí)現(xiàn)預(yù)覽效果的相關(guān)資料,需要的朋友可以參考下2016-01-01
Android Parcelable與Serializable詳解及區(qū)別
這篇文章主要介紹了Android Parcelable與Serializable詳解及區(qū)別的相關(guān)資料,需要的朋友可以參考下2017-01-01
Android媒體通知欄多系統(tǒng)適配實(shí)例講解
對(duì)于Android來說其中一項(xiàng)很方便的操作便是下拉菜單,下拉菜單欄可以快捷打開某項(xiàng)設(shè)置,這篇文章主要給大家介紹了關(guān)于Android通知欄增加快捷開關(guān)的功能實(shí)現(xiàn),需要的朋友可以參考下2023-04-04
Android使用Javamail發(fā)送Email群發(fā)加附件
這篇文章主要為大家詳細(xì)介紹了Android使用Javamail發(fā)送Email群發(fā)加附件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01

