ANDROID VIEW MODEL学习记录

VIEW MODEL 介绍

  • 使用VIEW MODEL 保存的数据只会在系统重启,或内存吃紧被杀掉时数据才会丢失。

创建VEW MODEL实体对象

package com.example.wechatredpacket.view_model;

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

//https://blog.csdn.net/csdnzouqi/article/details/107446703
//MutableLiveData<String> mutableLiveData = null;
//mutableLiveData= userViewModel.getUserName();
//mutableLiveData.setValue(userName);
//String username = userViewModel.getUserName().getValue();
public class UserViewModel extends ViewModel {
    public static final String TAG = "UserViewModel";


    //token 这个数据最终是保存在数据中的,保存在ViewModel中是处于性能的考虑。
    private String token;

    // count 每次任务数量,默认为10;
    private MutableLiveData<Integer> count;

    //用户名
    private MutableLiveData<String> userName;
    //密码
    private MutableLiveData<String> pwd;

    public MutableLiveData<String> getUserName() {
        if (userName == null) {
            userName = new MutableLiveData<>();
            userName.setValue("");
        }
        return userName;
    }

    public MutableLiveData<String> getPwd() {
        if (pwd == null) {
            pwd = new MutableLiveData<>();
            pwd.setValue("");
        }
        return pwd;
    }

    public void setUserName(MutableLiveData<String> userName) {
        this.userName = userName;
    }

    public void setPwd(MutableLiveData<String> pwd) {
        this.pwd = pwd;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getToken() {
        return token;
    }

    public MutableLiveData<Integer> getCount() {
        if (count == null) {
            count = new MutableLiveData<>();
            count.setValue(10);
        }
        return count;
    }

    public void setCount(MutableLiveData<Integer> count) {
        this.count = count;
    }

}

在Fragment中实例化VIEW MODEL

      userViewModel = new ViewModelProvider(getActivity(), new ViewModelProvider.NewInstanceFactory()).get(UserViewModel.class);
        //旧的写法  MyViewModel model = ViewModelProviders.of(activity).get(MyViewModel.class);
		// contactsViewModel = new ViewModelProvider(getActivity()).get(ContactsViewModel.class);
        FragmentLoginBinding binding;
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_login, container, false);\
        //赋值
        binding.setUserViewModel(userViewModel);
        //绑定生命周期
        binding.setLifecycleOwner(getActivity());

XML


<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
            name="UserViewModel"
            type="com.example.wechatredpacket.view_model.UserViewModel" />
    </data>
            <EditText
                android:text="@{String.valueOf(UserViewModel.userName)}"
                 />
</layout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值