一、前言
LiveData 是一个可观察的数据存储类, 并且具有Activity or Framgent生命周期的感知能力,livedata会将存储的数据,发送给正处于活跃的观察者observer。
使用LiveData的优势
UI和数据保持一致
避免内存泄漏
不会因 Activity 停止而导致崩溃
自动感知生命周期
解决Configuration changes问题
共享资源:使用单例扩展LiveData
二、相关API
1、postValue()
1.此方法可以在主线程或者子线程中调用,最终会在主线程执行
2.如果在主线程执行发布的任务之前多次调用此方法,则仅将分配最后一个值
3.如果同时调用 .postValue(“a”)和.setValue(“b”),最终值b被值a覆盖
2、setValue()
1.此方法只能在主线程里调用
3、getValue()
返回当前值。 注意,在后台线程上调用此方法并不能保证将接收到最新的值。
4、removeObserver(@NonNull final Observer<? super T> observer)
移除指定的观察者
5、removeObservers(@NonNull final LifecycleOwner owner)
移除当前Activity或者Fragment的全部观察者
6、 hasActiveObservers()
如果此LiveData具有活动(Activity或者Fragment在前台,当前屏幕显示)的观察者,则返回true。其实如果这个数据的观察者在最前台就返回true,否则false。
7、 hasActiveObservers()
如果此LiveData具有观察者,则返回true。
8、observe(@NonNull LifecycleOwner owner, @NonNull Observer<? super T> observer)
设置此LiveData数据当前activity或者Fragment的观察者,会给此activity或者Fragment在前台时回调数据。
9、observeForever(@NonNull Observer<? super T> observer)
1.设置永远观察者,永远不会被自动删除。您需要手动调用removeObserver(Observer)以停止观察此LiveData,
2.设置后此LiveData,一直处于活动状态,不管是否在前台哪里都会获得回调。
三、基本使用
1、导入lifecycle相关包
def lifecycle_version = "2.2.0"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
2>自定义bean:ReboundBean
public class ReboundBean {
private String num;
private long time;
private int count;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
}
3>定义ViewModle
public class BlueDeviceModel extends ViewModel {
public List<String> blueDeviceList;
public List<ReboundBean> reboundBeanList;
public MutableLiveData<ReboundBean> reboundBeanMutableLiveData;
//发送数据
public void updateReboundData(ReboundBean reboundBean){
reboundBeanMutableLiveData.postValue(reboundBean);
}
public MutableLiveData<ReboundBean> getReboundBeanMutableLiveData() {
if (reboundBeanMutableLiveData == null) {
reboundBeanMutableLiveData= new MutableLiveData<ReboundBean>();
}
return reboundBeanMutableLiveData;
}
}
4>在activity中的使用
//创建一个Observer观察UI
final Observer<ReboundBean> reboundBeanObserver=new Observer<ReboundBean>() {
@Override
public void onChanged(ReboundBean reboundBean) {
mReboundAdapter.getReboundList().add(reboundBean);
mReboundAdapter.notifyItemInserted(0);
recyclerView.smoothScrollToPosition(0);
}
};
注册监听
BlueDeviceModel blueDeviceModel=new ViewModelProvider(this).get(BlueDeviceModel.class);
//注册观察liveData
blueDeviceModel.getReboundBeanMutableLiveData().observe(this,reboundBeanObserver);
在接口返回中发送数据
JSONObject jsonObject=new JSONObject(jsonString);
ReboundBean rebound = new ReboundBean();
rebound.setTime((Long) jsonObject.get("currentTime"));
rebound.setNum(String.valueOf(jsonObject.getInt("num")));
rebound.setCount(mReboundAdapter.getReboundList().size() + 1);
blueDeviceModel.updateReboundData(rebound);