Andriod --- JetPack :DataBinding + LiveData +ViewModel 简单实例

Andriod — JetPack :初识 JetPack

Andriod — JetPack :LifeCycle 的诞生

Andriod — JetPack :ViewModel 的诞生

Andriod — JetPack :BaseObservable 与 ObservableField 双向绑定

Andriod — JetPack :DataBinding + LiveData +ViewModel 简单实例

Andriod — JetPack :Room 增删改查

Andriod — JetPack :Room + ViewModel + LiveData 增删改查实例

Andriod — JetPack :LiveData setValue 和 postValue 的区别

一、DataBinding

1.让布局页面承担了部分原本属于页面的工作,是页面文件和布局耦合度进一步降低。页面文件:Activity。布局:activity_main.xml

2.减少了很多 findViewById 与 OnClickListener
布局文件可以包含一些简单的业务逻辑
Andriod --- JetPack :DataBinding + LiveData +ViewModel 简单实例_第1张图片

二、LiveData

在 ViewModel 中的数据发生变化时通知页面
Andriod --- JetPack :DataBinding + LiveData +ViewModel 简单实例_第2张图片LiveData 与 ObservableField 同样可以实现双向绑定
这里的 Model 层下面的例子未呈现,等过后学到了 JetPack - Room 自然就懂了

三、ViewModel

1.它是介于 View(视图)和 Model(数据模型)直接的桥梁
2.使视图和数据能够分离,也能够保持通信
https://blog.csdn.net/qq_43290288/article/details/123478346

四:DataBinding + LiveData +ViewModel 实例

在 build.gradle 中添加 DataBinding 支持

android {
    dataBinding {
        enabled = true
    }
}

自定义MyViewModel.java

import android.view.View;

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

public class MyViewModel extends ViewModel {
    private MutableLiveData<Integer> aTeamScore;
    private MutableLiveData<Integer> bTeamScore;

    public MutableLiveData<Integer> getaTeamScore() {
        if(aTeamScore == null) {
            aTeamScore = new MutableLiveData<>();
            aTeamScore.setValue(0);
        }
        return aTeamScore;
    }
    public MutableLiveData<Integer> getbTeamScore() {
        if(bTeamScore == null) {
            bTeamScore = new MutableLiveData<>();
            bTeamScore.setValue(0);
        }
        return bTeamScore;
    }
    public void aTeamAdd(View view) {
        aTeamScore.setValue(aTeamScore.getValue() + 1);
    }
    public void bTeamAdd() {
        bTeamScore.setValue(bTeamScore.getValue() + 1);
    }
}

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import android.database.DatabaseUtils;
import android.os.Bundle;

import com.example.mvvm20220312.databinding.ActivityMainBinding;
import com.example.mvvm20220312.viewmodel.MyViewModel;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); // ActivityMainBinding 对应 activity_main.xml
        MyViewModel viewModel  = new ViewModelProvider(this, new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(MyViewModel.class);
        binding.setViewModel(viewModel); // DataBinding 与 ViewModel 绑定
        binding.setLifecycleOwner(this); // 要想在绑定类中使用 LiveData 对象,我们需要指定生命周期所有者来定义 LiveData 对象的范围。在绑定类实例化后指定 activity 作为生命周期所有者
    }
 }

activity_main.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"
    tools:context=".MainActivity">
    
    <data>
        <variable
            name="viewModel"
            type="com.example.mvvm20220312.viewmodel.MyViewModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{String.valueOf(viewModel.getaTeamScore())}"
            android:textSize="36sp"
            tools:text="10"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.252"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.174" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{String.valueOf(viewModel.getbTeamScore())}"
            android:textSize="36sp"
            tools:text="10"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.588"
            app:layout_constraintStart_toEndOf="@+id/textView2"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.174" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.207"
            android:onClick="@{viewModel::aTeamAdd}"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView2"
            android:text="蓝队"
            app:layout_constraintVertical_bias="0.499" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="红队"
            android:onClick="@{() -> viewModel.bTeamAdd()}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.755"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView3"
            app:layout_constraintVertical_bias="0.499" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

Andriod --- JetPack :DataBinding + LiveData +ViewModel 简单实例_第3张图片

学习内容来自动脑学院~

你可能感兴趣的:(Android,项目开发笔记,android,JetPack,DataBinding,LiveData,ViewModel)