前言:LiveData的介绍
LiveData 是一种可观察的数据存储器类。与常规的可观察类不同,LiveData 具有生命周期感知能力,意指它遵循其他应用组件(如 Activity、Fragment 或 Service)的生命周期。这种感知能力可确保 LiveData 仅更新处于活跃生命周期状态的应用组件观察者。
下面我们通过LiveData做一个简单的例子,来理解观察数据发生变化的功能。
一、首先引入依赖
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
二、activity_live_data_test.xml布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context=".LiveDataTestActivity"> <TextView android:id="@+id/tv_text" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginTop="100dp" android:gravity="center" android:textSize="30sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="200dp" android:gravity="center" android:orientation="horizontal"> <ImageButton android:id="@+id/img_button_add" android:layout_width="70dp" android:layout_height="70dp" android:layout_marginRight="20dp" app:srcCompat="@drawable/ic_sharp_thumb_up_24" /> <ImageButton android:id="@+id/img_button_subtract" android:layout_width="70dp" android:layout_height="70dp" android:layout_marginLeft="20dp" app:srcCompat="@drawable/ic_baseline_thumb_down_24" /> </LinearLayout> </LinearLayout>
三、创建ViewModelWithLiveData类继承于ViewModel,在这个类里面声明变量,具体讲解已经在代码中给出
public class ViewModelWithLiveData extends ViewModel { //声明一个变量 MutableLiveData是容器 Integer是变量的类型 private MutableLiveData<Integer> LikedNumber; public MutableLiveData<Integer> getLikedNumber() { if (LikedNumber == null) { //LikedNumber是对象类型,不是基本数据类型,所以要保证变量不是空的 LikedNumber = new MutableLiveData<>(); //初始化为0 LikedNumber.setValue(0); } return LikedNumber; } public void addLikedNumber(int n) { LikedNumber.setValue(LikedNumber.getValue() + n); } }
四、给viewModelWithLiveData里面的变量添加一个观察,点击按钮实现+1,-1操作
public class LiveDataTestActivity extends AppCompatActivity { private ViewModelWithLiveData viewModelWithLiveData; private TextView tv_text; private ImageButton img_button_add; private ImageButton img_button_subtract; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_live_data_test); tv_text = findViewById(R.id.tv_text); img_button_add = findViewById(R.id.img_button_add); img_button_subtract = findViewById(R.id.img_button_subtract); viewModelWithLiveData = new ViewModelProvider(this).get(ViewModelWithLiveData.class); //给viewModelWithLiveData里面的变量添加一个观察 观察它自己 如果数据发生变化,则呼叫下面的函数 //observe()的第一个参数:需要具有LifeCycle管理功能的一些对象 Activity就是具有管理LifeCycle功能的对象 //系统已经帮我们做好了,不用在其他地方取消观察 只需要添加观察就可以了。 viewModelWithLiveData.getLikedNumber().observe(this, new Observer<Integer>() { //当数据发生改变的时候 呼叫这个函数 @Override public void onChanged(Integer integer) { tv_text.setText(String.valueOf(integer)); } }); img_button_add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { viewModelWithLiveData.addLikedNumber(1); } }); img_button_subtract.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { viewModelWithLiveData.addLikedNumber(-1); } }); } }
效果演示:
旋转屏幕,切换系统语言等配置数据不会丢失,这就是本篇内容所要介绍的内容,有不当处,可以在评论区指正哈~