veData是一种可观察到数据存储器类,LiveData具有感知生命周期的能力,确保LiveData仅更新处于活跃生命周期状态的观察者。
优点:
确保界面符合数据状态,当生命周期状态变化时,LiveData通知Observer,可以在observer中更新界面。观察者可以在生命周期状态更改时刷新界面,而不是在每次数据变化时刷新界面。
不会发生内存泄漏,observer会在LifecycleOwner状态变为DESTROYED后自动remove。
不会因 Activity 停止而导致崩溃,如果LifecycleOwner生命周期处于非活跃状态,则它不会接收任何 LiveData事件。
不需要手动解除观察,开发者不需要在onPause或onDestroy方法中解除对LiveData的观察,因为LiveData能感知生命周期状态变化,所以会自动管理所有这些操作。
数据始终保持最新状态,数据更新时 若LifecycleOwner为非活跃状态,那么会在变为活跃时接收最新数据。例如,曾经在后台的 Activity 会在返回前台后,observer立即接收最新的数据。
setValue() 主线程更新数据
postValue() 子线程更新数据
observe() 添加观察者
onChanged() 接收数据
<?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=".livedate.LiveDataActivity">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="224dp" />
<Button
android:id="@+id/btn_cxk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CXK"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guideline"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_jay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Jay"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_cxk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintBottom_toTopOf="@+id/btn_cxk"
app:layout_constraintEnd_toStartOf="@+id/guideline"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_jay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintBottom_toTopOf="@+id/btn_jay"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.testapplication.livedate;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
// 第一步建立 类
public class LiveDataViewModel extends ViewModel {
private MutableLiveData<Integer> tickCxk,ticketJay;
// MutableLiveData
public MutableLiveData<Integer> getTickCxk(){
if(tickCxk == null){
tickCxk = new MutableLiveData<>();
tickCxk.setValue(0);
}
return tickCxk;
}
public MutableLiveData<Integer> getTicketJay(){
if(ticketJay == null){
ticketJay = new MutableLiveData<>();
ticketJay.setValue(0);
}
return ticketJay;
}
public void addJay(){
if(ticketJay.getValue() !=null){
ticketJay.setValue(ticketJay.getValue() +1);
}
}
public void addCxk(){
if(tickCxk.getValue() != null){
tickCxk.setValue(tickCxk.getValue()+1);
}
}
}
package com.example.testapplication.livedate;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.example.testapplication.R;
import com.example.testapplication.R2;
public class LiveDataActivity extends AppCompatActivity {
private TextView mCxkTv,mJayTv;
private LiveDataViewModel viewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live_data);
viewModel = new ViewModelProvider(this).get(LiveDataViewModel.class);
mCxkTv = findViewById(R.id.tv_cxk);
mJayTv = findViewById(R.id.tv_jay);
viewModel.getTicketJay().observe(this, new Observer<Integer>() {
@Override
public void onChanged(Integer integer) {
mJayTv.setText(String.valueOf(viewModel.getTicketJay().getValue()));
}
});
viewModel.getTickCxk().observe(this, new Observer<Integer>() {
@Override
public void onChanged(Integer integer) {
mCxkTv.setText(String.valueOf(viewModel.getTickCxk().getValue()));
}
});
findViewById(R.id.btn_cxk).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
viewModel.addCxk();
}
});
findViewById(R.id.btn_jay).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
viewModel.addJay();
}
});
}
}