使用LiveData和ViewModel为Android项目搭建MVVM架构(Kotlin语言版)(入门教程)

  • 写在前面
    现在MVVM架构大行其道,逐渐取代MVP架构成为Android开发的主流架构,并且google官方为了方便实现MVVM架构推出了Architecture系列的库,现已纳入到jetpack中,并且推出了KTX版本的,这样用Kotlin语言也可以更方便的使用Architecture了。但网上的教程大部分是JAVA语言版本的,而且很多就是按照官方文档翻译一下,并没有讲的很清楚。
    Kotlin语言和MVVM架构必然是将来Android开发的主流趋势,早一天学会,就能走在别人前面!
    下面我用一个完整的小项目实例带各位学习使用Kotlin语言如何开发MVVM
    既然是入门教程,我会用尽量少的代码和尽量详细的文字说明,如果后续想继续深入学习的,请继续关注我的教程
    话不多说,现在就开始吧!

  • 项目配置
    首先确保你的AndroidStudio版本不低于3.2
    创建一个新项目,创建时勾选include kotlin support(勾选了此选项,AndroidStudio会自动给你配置Kotlin开发环境),接着选择创建一个empty Activity
    项目创建完成之后需要做一定的配置
    1.为了将来使用ktx,我们需要先打开androidx,找到工程目录下面gradle.properties文件,添加以下两行

    android.useAndroidX=true
    android.enableJetifier=true
    

    2.打开app目录底下build.gradle,将compileSdkVersion改成28
    在dependencies下面,将

     	implementation 'com.android.support:appcompat-v7:28.0.0'
     	implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    

    这两行干掉,替换成androidx版本的

    	implementation 'androidx.appcompat:appcompat:1.1.0-alpha01'
    	implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    

    添加LiveData和ViewModel依赖库(这也是androidx版本的)

    	implementation "androidx.lifecycle:lifecycle-extensions:2.1.0-alpha01"
    

    最后sync一下,就算配置完成了

  • 布局文件:
    为了简单起见,我们设计一个最简单的功能,就是展示书籍名称和作者
    然后通过输入添加,来模拟数据变化,然后看看怎么通过MVVM来实现它
    布局不是本文的重点,我就直接贴代码出来了
    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <TextView
            android:id="@+id/textView_books"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintHeight_percent="0.55"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:scrollbars="vertical"
            android:textAppearance="@style/TextAppearance.AppCompat.Large"
            tools:text="《三体》 - 刘慈欣"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />
    
        <EditText
            android:id="@+id/editText_book_name"
        
  • 12
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值