0
点赞
收藏
分享

微信扫一扫

android viewmodel第二集 livedata基本使用


前言

各位同学大家好, 最近在学习livedata的基础知识所以就分享给大家 那么废话不多说我们正式开始。

效果图:

android viewmodel第二集 livedata基本使用_ide

android viewmodel第二集 livedata基本使用_android studio_02

android viewmodel第二集 livedata基本使用_xml_03

具体实现:

我们可以观察上面的案例 我们点击button 个一秒钟 我们的textview 会自增1 而且横竖屏切换的时候 不会受到影响导致数据丢失:

创建 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;
}

}

创建我们的 MyViewModel 继承 ViewModel 然后定义 private MutableLiveDatacurrentSecond; 变量 和 getCurrentSecond 方法 并初始化设置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>

布局效果

android viewmodel第二集 livedata基本使用_android studio_04

具体逻辑

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();
}
});
}

实例化我们 viewmodel 后通过 observe 拿掉回调结果然后显示在UI textview 上面

定时器逻辑

private  void  startTime(){
new Timer().schedule(new TimerTask() {
@Override
public void run() {
viewModel.getCurrentSecond().postValue(viewModel.getCurrentSecond().getValue()+1);
}
},1000,1000);
}

我们设置 一秒钟执行一次 并且去更新 value 的值 这样当我们点击button点击事件的时候 我们的定时器就每间隔一秒钟自增一 而且我们在横竖屏切换我们textview 也没没有数据丢失清零。

最后总结:

对比常规的写法我们写一个变量赋值 我们livedata 很好保存了 瞬时数据 并且在界面旋转的时候不会丢失 解决瞬态数据丢失 的问题 非常的直观 而且代码也不多很好的解决了我们实际的问题 那么其他viewmodel特性 我会在后面的章节里面一一讲到 。最后希望我的文章能帮助到各位解决问题 ,以后我还会贡献更多有用的代码分享给大家。各位同学如果觉得文章还不错 ,麻烦给关注和star,小弟在这里谢过啦!



举报

相关推荐

0 条评论