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库。。。。。。。。。。。

相关推荐
alexhilton4 小时前
使用FunctionGemma进行设备端函数调用
android·kotlin·android jetpack
冬奇Lab7 小时前
InputManagerService:输入事件分发与ANR机制
android·源码阅读
张小潇10 小时前
AOSP15 Input专题InputManager源码分析
android·操作系统
lhDream11 小时前
Kotlin 开发者必看!JetBrains 开源 LLM 框架 Koog 快速上手指南(含示例)
kotlin
RdoZam12 小时前
Android-封装基类Activity\Fragment,从0到1记录
android·kotlin
奥陌陌17 小时前
android 打印函数调用堆栈
android
用户9851200358317 小时前
Compose Navigation 3 深度解析(二):基础用法
android·android jetpack
恋猫de小郭17 小时前
Android 官方正式官宣 AI 支持 AppFunctions ,Android 官方 MCP 和系统级 OpenClaw 雏形
android·前端·flutter
黄林晴18 小时前
Android 17 Beta 2,隐私这把锁又拧紧了
android
Kapaseker19 小时前
研究表明,开发者对Kotlin集合的了解不到 20%
android·kotlin