Kotlin jetpack MVP

开启dataBinding

app build.gradle

复制代码
android {
	buildFeatures {
        dataBinding true
    }
}
  • 一些依赖库

    复制代码
      def lifecycle_version = "2.4.1"
      // https://developer.android.google.cn/jetpack/androidx/releases/lifecycle?hl=en
      // LiveData
      api "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
      // ViewModel
      api "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
      // optional - ProcessLifecycleOwner provides a lifecycle for the whole application process
      api "androidx.lifecycle:lifecycle-process:$lifecycle_version"
      // Lifecycles only (without ViewModel or LiveData)
      api "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
      // 在Activity中使用viewModels
      api "androidx.activity:activity-ktx:1.3.1"
      // 在Fragment中使用viewModels
      api "androidx.fragment:fragment-ktx:1.3.6"

基类

复制代码
abstract class BaseActivity<VDB : ViewDataBinding>(private var contentViewID:Int){
	protected lateinit var binding: VDB

	@CallSuper
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, contentViewID)
    }
}

子类

复制代码
class MainActivity : BaseActivity<ActivityMainBinding>(R.layout.activity_main){

    private val xVM: XxxxVM by viewModels()
    // 发起任务
  	xVM.getXx(xId)
	// 处理结果
	xVM.xLiveData.observe(this) { bid ->
    }
}

xml布局

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

    <data>

    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <include
            android:id="@+id/tabLayout"
            layout="@layout/tab_main_activity" />

    </LinearLayout>
</layout>

处理任务的ViewMode

复制代码
class BindBrandVM : ViewModel() {
    val xLiveData = MutableLiveData<String>()

    fun getXx(x: String) {
        RetrofitManager.api.xxxxx(FormBody.Builder()
            .add("xxx", x)
            .build())
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : BaseSubscriber<BaseHttpEntity<XxxxRes>>() {
                override fun onErr(throwable: Throwable) {}
                override fun onNext(res: BaseHttpEntity<XxxxRes>) {
                    super.onNext(res)
                    try {
                        if (res.result != null) {
                            val bid = res.result!!.bid
                            xLiveData.value = bid
                        }
                    } catch (e: java.lang.Exception) {
                    }
                }
            })
    }
}

还可以添加其他jetpack库。。。。。。。。。。。

相关推荐
千里马学框架4 天前
一起学 Android 14:ShellTransition 屏幕旋转过程深度剖析
android·智能手机·性能优化·framework·性能·屏幕旋转·rotation
美狐美颜SDK开放平台4 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
AFinalStone4 天前
Android7 SystemUI源码解析(七)Keyguard锁屏模块深度解析
android·systemui
致远ccc4 天前
Google Play 上架前如何测试 App?多国家 Android 环境测试
android·app测试·googleplay·多国家应用测试
ttyyttemo4 天前
Kotlin 协程中的 Job 结构化并发与取消
android
sun0077004 天前
tbox 4g/5g切换,导致wan ip 改变,导致车机旧网络不可用。需要重启车机才行
android
ai2work4 天前
ch23 综合复刻:从零做一个最小可用版本(capstone)
kotlin
其实防守也摸鱼4 天前
内网穿透与反向代理:原理、工具与实战指南
android·大数据·运维·安全·网络安全·自动化·渗透
ai2work4 天前
ch21 签名、校验与发版
kotlin
AFinalStone4 天前
Android7 SystemUI 源码解析(四)NavigationBar 导航栏与 SystemBars
android·systemui