前言
各位同學(xué)大家好狱从, 最近在學(xué)習(xí)livedata的基礎(chǔ)知識(shí)所以就分享給大家 那么廢話不多說我們正式開始商玫。
效果圖:
具體實(shí)現(xiàn):
我們可以觀察上面的案例 我們點(diǎn)擊button 個(gè)一秒鐘 我們的textview 會(huì)自增1 而且橫豎屏切換的時(shí)候 不會(huì)受到影響導(dǎo)致數(shù)據(jù)丟失:
-
創(chuàng)建 MyViewModel
package com.cbhx.livedatademo;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
public class MyViewModel extends ViewModel {
private MutableLiveData<Integer>currentSecond;
public MutableLiveData<Integer>getCurrentSecond(){
if(currentSecond==null){
currentSecond=new MutableLiveData<>();
currentSecond.setValue(0);
}
return currentSecond;
}
}
創(chuàng)建我們的 MyViewModel 繼承 ViewModel 然后定義 private MutableLiveData<Integer>currentSecond; 變量 和 getCurrentSecond 方法 并初始化設(shè)置value 為0
MainActivity 邏輯
-
布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginStart="151dp"
android:layout_marginTop="99dp"
android:layout_marginEnd="166dp"
android:textSize="15dp"
android:text="開始"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>
-
布局效果
具體邏輯
protected MyViewModel viewModel;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
viewModel = new ViewModelProvider(this, new
ViewModelProvider.AndroidViewModelFactory(getApplication())).get(MyViewModel.class);
viewModel.getCurrentSecond().observe(this, new Observer<Integer>() {
@Override
public void onChanged(Integer integer) {
textView.setText(String.valueOf(integer));
}
});
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startTime();
}
});
}
實(shí)例化我們 viewmodel 后通過 observe 拿掉回調(diào)結(jié)果然后顯示在UI textview 上面
定時(shí)器邏輯
private void startTime(){
new Timer().schedule(new TimerTask() {
@Override
public void run() {
viewModel.getCurrentSecond().postValue(viewModel.getCurrentSecond().getValue()+1);
}
},1000,1000);
}
我們設(shè)置 一秒鐘執(zhí)行一次 并且去更新 value 的值 這樣當(dāng)我們點(diǎn)擊button點(diǎn)擊事件的時(shí)候 我們的定時(shí)器就每間隔一秒鐘自增一 而且我們在橫豎屏切換我們textview 也沒沒有數(shù)據(jù)丟失清零灿巧。
最后總結(jié):
對比常規(guī)的寫法我們寫一個(gè)變量賦值 我們livedata 很好保存了 瞬時(shí)數(shù)據(jù) 并且在界面旋轉(zhuǎn)的時(shí)候不會(huì)丟失 解決瞬態(tài)數(shù)據(jù)丟失 的問題 非常的直觀 而且代碼也不多很好的解決了我們實(shí)際的問題 那么其他viewmodel特性 我會(huì)在后面的章節(jié)里面一一講到 砚哗。最后希望我的文章能幫助到各位解決問題 缸濒,以后我還會(huì)貢獻(xiàn)更多有用的代碼分享給大家锅知。各位同學(xué)如果覺得文章還不錯(cuò) 妇拯,麻煩給關(guān)注和star幻馁,小弟在這里謝過啦!