其他分享
首页 > 其他分享> > 简易版LiveData-->实现MVVM

简易版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);
    }
}

标签:MVVM,observer,lifecycleOwner,--,void,viewModel,简易版,import,public
来源: https://blog.csdn.net/yangzhaomuma/article/details/121562115