• Kotlin MVVM之Jetpack系列ViewModel、LiveData的简单使用


     

    一、MVVM是什么?

    MVVM分为Model,View,ViewModel 三个部分

    Model:数据层,包含数据实体和对数据实体的操作

    View:UI层,对应于Activity,XML,负责数据显示以及用户交互。

    ViewModel:中间层,作为中间桥梁 去通知model数据层处理数据业务,并将结果通知给 UI 层处理 UI 逻辑。ViewModel中只有Activity持有ViewModel引用,ViewModel是不持有Activity的引用的。ViewModel生命周期大于Activity,如果持有Activity的引用,容易引起内存泄漏。ViewModel结合Jetpack的LiveData通过观察者回调的方式,在数据更新时通知UI层。

    二、使用步骤:

    1.配置gradle

    implementation 'androidx.lifecycle:lifecycle-viewmodel:2.3.0'

    2.继承ViewModel类

    class RecordMapViewModel : ViewModel()

    3.Activity中调用ViewModel获取数据

    val viewModel=ViewModelProvider(this).get(RecordMapViewModel::class.java)

    三、代码实例:

      RecordMapViewModel

    1. class RecordMapViewModel : ViewModel() {
    2. private val mMLDLDSweepMap: MutableLiveData by lazy {
    3. MutableLiveData()
    4. }
    5. private val mIRecordMapModel: IRecordMapModel by lazy {
    6. RecordMapModel()
    7. }
    8. fun getMLDLDSweepMap() = mMLDLDSweepMap
    9. fun getMapData(bucket: String, path: String) {
    10. mIRecordMapModel.getMapData(bucket, path, object : ICallback {
    11. override fun callback(data: Any?) {
    12. ProgressUtil.hideLoading()
    13. if (data is LDSweepMap) {
    14. mMLDLDSweepMap.postValue(data)
    15. }
    16. }
    17. })
    18. }
    19. }

    RecordMapViewModel

    1. class RecordMapModel : IRecordMapModel {
    2. companion object {
    3. @JvmStatic
    4. private val TAG = "RecordMapModel"
    5. }
    6. override fun getMapData(bucket: String, path: String, callback: ICallback) {
    7. LDSweeper.getInstance().tuyaSweeper?.getSweeperByteData(bucket, path, object : ITuyaByteDataListener {
    8. override fun onSweeperByteData(data: ByteArray?) {
    9. LogUtil.i(TAG, "getSweeperByteData success")
    10. callback.callback(DataUtil.byteToLDSweepMap(data))
    11. }
    12. override fun onFailure(code: Int, msg: String?) {
    13. LogUtil.i(TAG, "getSweeperByteData onFailure: $msg")
    14. callback.callback(null)
    15. }
    16. });
    17. }
    18. }

    IRecordMapModel

    1. interface IRecordMapModel {
    2. fun getMapData(bucket: String, path: String, callback: ICallback)
    3. }

    RecordMapActivity

    1. public class RecordMapActivity extends BaseActivity {
    2. private final String TAG = "RecordMapActivity";
    3. RecordMapViewModel mRecordMapViewModel;
    4. @Override
    5. protected void onCreate(Bundle savedInstanceState) {
    6. super.onCreate(savedInstanceState);
    7. mRecordMapViewModel = new ViewModelProvider(this).get(RecordMapViewModel.class);
    8. initData();
    9. }
    10. private void initData() {
    11. MutableLiveData<LDSweepMap> mapMutableLiveData = mRecordMapViewModel.getMLDLDSweepMap();
    12. mapMutableLiveData.observe(this, new Observer<LDSweepMap>() {
    13. @Override
    14. public void onChanged(LDSweepMap ldMap) {
    15. }
    16. });
    17. mRecordMapViewModel.getMapData(bucket, file);
    18. }
    19. }

  • 相关阅读:
    MyBatis 动态SQL与分页
    maven打包可运行jar
    基于词典的正向最大匹配和逆向最大匹配中文分词
    TCP 中的 Delay ACK 和 Nagle 算法
    springboot和springcloudAlibaba的版本对应关系
    const和constexpr记录
    书店系统的设计与实现
    Ficow 的 AI 平台快速上手指南(ChatGPT, NewBing, ChatGLM-6B, cursor.so)
    一、Git介绍、以及原理
    jenkins
  • 原文地址:https://blog.csdn.net/sziitjin/article/details/127884573