欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Android使用JetPack入门实例

程序员文章站 2022-04-01 18:54:56
概念Android最流行框架-JetPack,采用MVVM的设计模式,数据流如下图所示。程序实例gradle添加依赖androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"MainActivity.javapublic class MainAc...

概念

Android最流行框架-JetPack,采用MVVM的设计模式,数据流如下图所示。

Android使用JetPack入门实例

程序实例

gradle添加依赖

androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"

MainActivity.java

public class MainActivity extends AppCompatActivity {
    MyViewModel myViewModel;
    ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding= DataBindingUtil.setContentView(this, R.layout.activity_main);
        myViewModel= new ViewModelProvider(this).get(MyViewModel.class);
        binding.setData(myViewModel);
        binding.setLifecycleOwner(this);
        myViewModel.getLiveNum();
    }
}

MyViewModel.java

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

    public MutableLiveData<Integer> getLiveNum() {
        if (liveNum==null) {
            liveNum=new MutableLiveData<>();
            liveNum.setValue(0);
        }
        return liveNum;
    }

    public void addLiveNum(int n) {
        liveNum.setValue(liveNum.getValue()+n);
    }
}

layout中使用viewModel

<?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="data"
            type="com.invensun.seven.ch34.MyViewModel" />

    </data>

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

        <TextView
            android:id="@+id/tvNum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="100dp"
            android:text="@{String.valueOf(data.liveNum)}"
            android:textSize="24sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/btnAdd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:text="+1"
            android:textSize="18sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintStart_toStartOf="parent"
            android:onClick="@{()->data.addLiveNum(1)}"
            app:layout_constraintTop_toBottomOf="@+id/tvNum" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

 

本文地址:https://blog.csdn.net/quietbxj/article/details/108844543

相关标签: Android