Skip to content
William edited this page Sep 11, 2024 · 11 revisions

介绍

  • 基于MVVM模式组件化集成谷歌官方推荐的JetPack组件库:DataBinding、LiveData、ViewModel、Lifecycle组件
  • 使用kotlin语言,添加大量拓展函数,简化代码
  • 加入Retrofit网络请求,协程,帮你简化各种操作,让你快速请求网络
  • 封装Base类,添加常用工具类
  • 搭配代码模板插件VLibraryPlugin使用开发效率翻倍
  • 如遇不会可参考该项目TTProject

使用集成

  • 1. 在root build.gradle中加入Jitpack仓库
allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
  • 2. 在app build.gradle中添加依赖
dependencies {
  ...
  implementation 'com.github.oooo7777777:VLibrary:latestVersion'
}
  • 3. 在app build.gradle中,android 模块下开启DataBinding

android {
    ...
   buildFeatures {
        dataBinding = true
    }
}
 
  • 4. 继承VBApplication
open class *** : VBApplication() {
    /**
     * 开启日志打印(日志TAG为 V_LOG)
     */
    override fun logConfig(): LogConfig {
        return LogConfig(this, true, true)
    }

    override fun initData() {
        //基类配置
        val vbOptions = VBConfigOptions.Builder()
            .build()
        VBConfig.init(vbOptions)

        //网络请求配置
        val netOptions = VBNetOptions.Builder()
            .setBaseUrl("https://www.wanandroid.com/")
            .setInterceptor(NetworkHeadInterceptor())
            .setExceptionHandling(NetworkExceptionHandling())
            .build()
        VBNetworkConfig.init(netOptions)
    }

}

继承基类

一般我们项目中都会有一套自己定义的符合业务需求的基类 VBActivity/VBFragment,所以我们的基类需要继承本框架的Base类

  • 使用Activity(基类为AppCompatActivity)
/**
 * @param MainActivityBinding DataBinding
 * @param VBBlankViewModel VBViewModel(如果该页面不使用ViewModel 使用BlankViewModel)
 */
class *** : VBActivity<MainActivityBinding, VBBlankViewModel>() {
    
 
}
  • 使用Fragment(基类为Fragment)
/**
 * @param MainFragmentBinding DataBinding
 * @param MeViewModel VBViewModel(如果该页面不使用ViewModel 使用BlankViewModel)
 */
class *** : VBFragment<MainFragmentBinding, VBBlankViewModel>() {

  
}
  • 使用ViewModel
class MainViewModel : VBViewModel() {
    
    
}
  • 使用Dialog(基类为Dialog)
class ***(mContext: Context) : VBDialog<***Binding>(mContext) {

 
}
Clone this wiki locally