简易版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 implements LifecycleObserver {

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


    public void setObserver(LifecycleOwner lifecycleOwner, Observer observer) {
        this.observer = observer;
        this.lifecycleOwner = lifecycleOwner;
        this.lifecycleOwner.getLifecycle().addObserver(this);
    }

    public void setObserver(Observer 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 {
        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 obserableAny;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        obserableAny = new ObserableAny<>();
        obserableAny.setObserver(this,new ObserableAny.Observer() {
            @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);
    }
}

你可能感兴趣的:(Android技巧,android)