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

Kotlin MVVM之Jetpack系列ViewModel、LiveData的简单使用_第1张图片

 

一、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

class RecordMapViewModel : ViewModel() {

    private val mMLDLDSweepMap: MutableLiveData by lazy {
        MutableLiveData()
    }

    private val mIRecordMapModel: IRecordMapModel by lazy {
        RecordMapModel()
    }

    fun getMLDLDSweepMap() = mMLDLDSweepMap

    fun getMapData(bucket: String, path: String) {
        mIRecordMapModel.getMapData(bucket, path, object : ICallback {
            override fun callback(data: Any?) {
                ProgressUtil.hideLoading()
                if (data is LDSweepMap) {
                    mMLDLDSweepMap.postValue(data)
                }
            }
        })
    }
}

RecordMapViewModel

class RecordMapModel : IRecordMapModel {
    companion object {
        @JvmStatic
        private val TAG = "RecordMapModel"
    }

    override fun getMapData(bucket: String, path: String, callback: ICallback) {
        LDSweeper.getInstance().tuyaSweeper?.getSweeperByteData(bucket, path, object : ITuyaByteDataListener {

            override fun onSweeperByteData(data: ByteArray?) {
                LogUtil.i(TAG, "getSweeperByteData success")
                callback.callback(DataUtil.byteToLDSweepMap(data))
            }

            override fun onFailure(code: Int, msg: String?) {
                LogUtil.i(TAG, "getSweeperByteData onFailure:  $msg")
                callback.callback(null)
            }
        });
    }
}

IRecordMapModel

interface IRecordMapModel {
    fun getMapData(bucket: String, path: String, callback: ICallback)
}

RecordMapActivity

public class RecordMapActivity extends BaseActivity {
    private final String TAG = "RecordMapActivity";

    RecordMapViewModel mRecordMapViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mRecordMapViewModel = new ViewModelProvider(this).get(RecordMapViewModel.class);
        initData();
    }

    private void initData() {
        MutableLiveData mapMutableLiveData = mRecordMapViewModel.getMLDLDSweepMap();
        mapMutableLiveData.observe(this, new Observer() {
            @Override
            public void onChanged(LDSweepMap ldMap) {
                
            }
        });
		
		mRecordMapViewModel.getMapData(bucket, file);
    }

}

你可能感兴趣的:(kotlin,Jetpack,kotlin,Jetpack)