admin管理员组文章数量:1630622
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
需要这份系统化的资料的朋友,可以戳这里获取
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
看到到上面的效果入 我们今天就用一个实际场景案例 用在点击屏幕中的加号button 上面文本一直累加 然后我们在旋转屏幕的时候 看一下数据会不会丢失。
具体效果实现
-
使用了viewmodel 的实现
- 没有旋转之前
- 旋转之后
我们可以观察手机屏幕显示的区域 我们显示数字text文本并没有丢失数据重置。 代码实现:
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android/apk/res/android"
xmlns:app="http://schemas.android/apk/res-auto"
xmlns:tools="http://schemas.android/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginTop="100dp"
android:textSize="30dp"
/>
<Button
android:id="@+id/plusNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginTop="100dp"
android:textSize="30dp"
android:text="+" />
</LinearLayout>
布局效果:
具体逻辑
我们定义一个viewmodel
package com.example.viewmodeldemo;
import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelStoreOwner;
/**
*
* 创建人:xuqing
* 创建时间:2022年5月2日18:28:14
* 类说明:myviewmodel
*
*/
public class MyViewModel extends ViewModel {
public int number;
}
然后定义一个number 值等下用于显示变化的text 文本
package com.example.viewmodeldemo;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
protected TextView textView;
private MyViewModel viewModel;
@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);
textView.setText(String.valueOf(viewModel.number));
findViewById(R.id.plusNumber).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textView.setText(String.valueOf(++viewModel.number));
}
});
}
}
我们通过ViewModelProvider 实例化最后get 到MyViewModel 的实例 然后我们在button的点击事件中去操作更新textview的变化 设置了number的自增 这时候我们去观察手机 我们旋转手机数据并没有丢失这就是我们viewmodel的特性
-
对比不适用viewmodel
我们可以看到不是用vewimodel 的效果 我们在旋转屏幕之后生命周期重新走 所以我们的text文本发生变化number数据丢失
- 布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android/apk/res/android"
xmlns:app="http://schemas.android/apk/res-auto"
xmlns:tools="http://schemas.android/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".TextActivity">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_gravity="center"
android:gravity="center"
android:textSize="30dp"
![img](https://img-blog.csdnimg/img_convert/72d08f5c87b8f4e077f0248b5d4d491b.png)
![img](https://img-blog.csdnimg/img_convert/bb910c0e4332f91ff6fbb9686aa678e2.png)
**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn/topics/618636735)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn/topics/618636735)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
版权声明:本文标题:HarmonyOS鸿蒙最全android ViewModel 基础(1),HarmonyOS鸿蒙如何构建简单的界面 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1729061255a1184426.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论