简易版LiveData-->实现MVVM

Posted 战国剑

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简易版LiveData-->实现MVVM相关的知识,希望对你有一定的参考价值。

遵循LiveData原理,摒弃其中的复杂逻辑,只取LiveData的响应更新能力,做一套简易版本的实现。

一、简易的ViewModel

package com.example.jetpacktest;

public class ViewModel 

    public String name;
    public int count;

二、简易的LiveData实现--命名为:ObserableAny

package com.example.jetpacktest;


import android.util.Log;

//如无需生命周期,可不引用androidx组件。这样就剩余纯响应式监听数据的逻辑
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.OnLifecycleEvent;

public class ObserableAny<T> implements LifecycleObserver 

    private static final String TAG = "ObserableAny";
    //观察者
    private Observer<T> observer;
    //生命周期
    private LifecycleOwner lifecycleOwner;
    //数据
    private T value;


    public void setObserver(LifecycleOwner lifecycleOwner, Observer<T> observer) 
        this.observer = observer;
        this.lifecycleOwner = lifecycleOwner;
        this.lifecycleOwner.getLifecycle().addObserver(this);
    

    public void setObserver(Observer<T> observer) 
        this.observer = observer;
    
    //setValue时触发更新
    public void setValue(T t) 
        value = t;
        if (observer != null) 
            observer.update(t);
        
    

    public T getValue() 
        return value;
    

    public interface Observer<T> 
        void update(T t);
    

    /*** 无需生命周期的时候,可去除以下代码 ***/
    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    void onResume() 
        Log.e(TAG, "OnLifecycleEvent onResume");
    

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    void onDestroy() 
        Log.e(TAG, "OnLifecycleEvent onDestroy");
        if(lifecycleOwner != null)
            lifecycleOwner.getLifecycle().removeObserver(this);
            lifecycleOwner = null;
        
        if(observer != null)
            observer = null;
        
    

三、简易实用

package com.example.jetpacktest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity 

    private ObserableAny<ViewModel> obserableAny;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        obserableAny = new ObserableAny<>();
        obserableAny.setObserver(this,new ObserableAny.Observer<ViewModel>() 
            @Override
            public void update(ViewModel viewModel) 
                Toast.makeText(MainActivity.this, buildDisplay(viewModel), Toast.LENGTH_SHORT).show();
            
        );
    

    public void test(View view) 
        ViewModel viewModel = new ViewModel();
        viewModel.name = "张三";
        viewModel.count = 1;
        obserableAny.setValue(viewModel);

    

    private String buildDisplay(ViewModel viewModel)
        return viewModel.name + " " + viewModel.count;
    

    public void test1(View view) 
        ViewModel viewModel = new ViewModel();
        viewModel.name = "李四";
        viewModel.count = 2;
        obserableAny.setValue(viewModel);
    

以上是关于简易版LiveData-->实现MVVM的主要内容,如果未能解决你的问题,请参考以下文章

简易版LiveData-->实现MVVM

简易版LiveData-->实现MVVM

Android MVVM:具有多个片段的活动 - 将共享 LiveData 放在哪里?

Android MVVM系列:LiveData的简单使用java版

如何在MVVM架构中观察RecyclerView适配器中的LiveData?

Android 上的 Kotlin:如何在片段中使用数据库中的 LiveData?