寒光博客

【Android】ViewModel LiveData小练习 感知数据变化,自动刷新页面
LiveDataTest Demo android 点击赞和踩ImageButton标签数据发生改变 退至后台,旋...
扫描右侧二维码阅读全文
21
2020/03

【Android】ViewModel LiveData小练习 感知数据变化,自动刷新页面

LiveDataTest Demo

android 点击赞和踩ImageButton标签数据发生改变 退至后台,旋转屏幕数据也不会发生改变(底层数据改变时,自动通知界面)

代码

ViewModelProviders的弃用

viewModelWithLiveData =ViewModelProviders.of(this).get(ViewModelWithLiveData.class);

可以改写为

viewModelWithLiveData = new ViewModelProvider(this).get(ViewModelWithLiveData.class);

同时在gradle dependencies 中添加: implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'

ViewModelWithLiveData.java

package com.example.livedatatest;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

public class ViewModelWithLiveData extends ViewModel {
    private MutableLiveData<Integer> LikedNumber;//容器 类型 私有类型
//使用构造器 不知道为啥程序会报错 毫无头绪
//    ViewModelWithLiveData() {//构造器 初始化数据 或者再get里判断
//        LikedNumber = new MutableLiveData<>();
//        LikedNumber.setValue(0);
//    }

    public MutableLiveData<Integer> getLikedNumber() {
        if (LikedNumber == null) {//初始化
            LikedNumber = new MutableLiveData<>();
            LikedNumber.setValue(0);
        }
        return LikedNumber;
    }

    public void addLikedNumber(int n) {
        LikedNumber.setValue(LikedNumber.getValue() + n);
    }
}

MainActivity.java

package com.example.livedatatest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    ViewModelWithLiveData viewModelWithLiveData;
    TextView textView;
    ImageButton imageButtonLike, imageButtonDisLike;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.textView);
        imageButtonLike = findViewById(R.id.imageButton);
        imageButtonDisLike = findViewById(R.id.imageButton2);
        viewModelWithLiveData = new ViewModelProvider(this).get(ViewModelWithLiveData.class);//viewModel和控制器绑定
//        viewModelWithLiveData = ViewModelProviders.of(this).get(ViewModelWithLiveData.class);//viewModel和控制器绑定 ViewModelProviders【api已被弃用】
        viewModelWithLiveData.getLikedNumber().observe(this, new Observer<Integer>() {//匿名内部类观察数据改变 给vm的liveData设置观察者 监听变化
            @Override
            public void onChanged(Integer integer) {
                textView.setText(String.valueOf(integer));//变化 view自动刷新
            }
        });

        imageButtonLike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModelWithLiveData.addLikedNumber(1);
            }
        });
        imageButtonDisLike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModelWithLiveData.addLikedNumber(-1);
            }
        });
    }
}
本文作者:Author:     文章标题:【Android】ViewModel LiveData小练习 感知数据变化,自动刷新页面
本文地址:https://dxoca.cn/StudyNotes/328.html       百度已收录
版权说明:若无注明,本文皆为“Dxoca's blog (寒光博客)”原创,转载请保留文章出处。
Last modification:March 21st, 2020 at 12:35 pm
如果觉得我的文章对你有用,请随意赞赏

Leave a Comment