01.Kotlin初始化

2020-03-09  本文已影响0人  LewisZhu

http://ddrv.cn/a/226675

0.建好项目之后 配置build.gradle,为了方便gradle的sync

https://www.jianshu.com/p/ad566d72a0c8


    repositories {
//        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
        maven { url 'https://maven.aliyun.com/repository/public' }
        maven { url 'https://maven.aliyun.com/repository/google' }
        maven { url 'https://maven.aliyun.com/repository/jcenter' }
        maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
        maven { url 'https://maven.aliyun.com/repository/central' }
        google()
        jcenter()
        maven { url "https://jitpack.io" }

    }

1>.迁移到androidX
https://blog.csdn.net/tuike/article/details/95937072

2>.activity中取得fragment

val fragment =  supportFragmentManager.findFragmentById(R.id.fragment_view)

3>.实体类的序列化
法一
先给AS装个插件:Parcelable Code Generator(for kotlin)


Screen Shot 2020-03-06 at 9.02.26 PM.png

intent.getParcelableExtra(OrderConstant.KEY_SHIP_ADDRESS)

法二
blog.jetbrains.com/kotlin/2017/08/kotlin-1-1-4-is-out/


Screen Shot 2020-03-07 at 11.46.10 PM.png Screen Shot 2020-03-07 at 11.45.24 PM.png

4>.启动图尺寸
竖屏
drawable-port-ldpi-screen 320 200
drawable-port-mdpi-screen 480 320
drawable-port-hdpi-screen 800 480
drawable-port-xhdpi-screen 1280 720
drawable-port-xxhdpi-screen 1600 960
drawable-port-xxxhdpi-screen 1920 1280

1.创建工程


Screen Shot 2020-02-07 at 4.00.27 PM.png

2.新建-module-library


Screen Shot 2020-02-07 at 4.01.42 PM.png
Screen Shot 2020-02-07 at 4.03.09 PM.png
3.转java工程为kotlin

tools-kotlin-configureKotlinInProject-android with gradle

4.application and library
变量可以放在gradle.properties中


Screen Shot 2020-02-07 at 8.20.27 PM.png
Screen Shot 2020-02-07 at 8.25.11 PM.png

5.AndroidManifest切换
1>.切换到project,在main中新建两个目录debug和release,里面分别放debug和release的manifest.
2>.放在build grade里面的

    sourceSets {
        //配置debugherelease的各个情况下  加载manifest
        main {
            if (isUserModule.toBoolean()) {
                manifest.srcFile 'src/main/release/AndroidManifest.xml'
//                release模式下排除debug文件夹中的所有java文件
//                java {
//                    exclude 'debug/**'
//                }
            } else {
                manifest.srcFile 'src/main/debug/AndroidManifest.xml'
            }
        }
    }

6.Android-extensions 视图插件(直接用id去操作控件)

配置:
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

7.Anko https://github.com/Kotlin/anko

Screen Shot 2020-02-07 at 9.21.46 PM.png
// Anko
implementation "org.jetbrains.anko:anko:$anko_version"

8.RxKotlin(提供了一些拓展方法,响应式编程)
RxAndroid(RxJava拓展,可以处理异步请求,兼容android特性,主线程UI事件)

RxKotlin

RxAdroid

配置:

1>版本
ext.rx_kotlin_version = '1.0.0'
ext.rx_android_version = ‘1.2.1'

2>.导入依赖
//RxKotlin
implementation "io.reactivex:rxkotlin:rx_kotlin_version" //RxAndroid implementation "io.reactivex:rxandroid:{rx_android_version}"

Observable很常用

9.Retrofit网络请求 和rx完美结合
https://www.jianshu.com/p/50b835c539b5

https://square.github.io/retrofit/
ext.ok_http_version = '3.4.1'
ext.retrofit_version = '2.1.0'
//Retrofit相关依赖
implementation "com.squareup.okhttp3:okhttp:{ok_http_version}" implementation "com.squareup.okhttp3:logging-interceptor:{ok_http_version}"
implementation "com.squareup.retrofit2:retrofit:{retrofit_version}" implementation "com.squareup.retrofit2:converter-gson:{retrofit_version}"
implementation "com.squareup.retrofit2:adapter-rxjava:${retrofit_version}"

使用:1>api 2>protocal request 3>repository

10.Dagger2注入神器:https://github.com/google/dagger
加入inject之后必须重新编译一下command+fn+F9
https://www.jianshu.com/p/5f11cacb6250

ext.dagger_version = '2.11'
apply plugin: 'kotlin-kapt'

//dagger2
implementation "com.google.dagger:dagger:dagger_version" implementation "com.google.dagger:dagger-android-support:dagger_version"
kapt "com.google.dagger:dagger-android-processor:dagger_version" kapt "com.google.dagger:dagger-compiler:dagger_version”

Screen Shot 2020-02-08 at 4.42.12 PM.png

11.RxLifecycle 解决Rx内存泄露

ext.rx_lifecycle_version = '1.0'
compile "com.trello:rxlifecycle-kotlin"
compile "com.trello:rxlifecycle-components"

12.recyclerview 防止内存泄露 在生命周期中自动断开与rx的连接
https://blog.csdn.net/qq_35585843/article/details/101309058

implementation 'androidx.recyclerview:recyclerview:1.0.0'

13.utils
1>.AppPrefsUtils
2>.NetWorkUtils
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
3>.GlideUtils
implementation 'com.github.bumptech.glide:glide:3.7.0'

14.Adapter
BaseRecyclerViewAdapter

15.widgets
1>.HeaderBar
2>.ProgressLoading

Android加载ttf字体

Android-Iconics
使用方法
使用自定义ttf

Screen Shot 2020-04-21 at 11.43.51 PM.png
  1. TabBar
    https://github.com/Ashok-Varma/BottomNavigation

17banner
https://github.com/youth5201314/banner

  1. Android-Coverflow
    https://github.com/crosswall/Android-Coverflow

Step 1\. Add the JitPack repository to your build file
allprojects {
    repositories {
            ...
            maven { url "https://jitpack.io" }
    }
}

Step 2\. Add the dependency
dependencies {
    compile 'com.github.crosswall:Android-Coverflow:release-v1.0.5'
}

20.上拉刷新
https://github.com/bingoogolapple/BGARefreshLayout-Android

21.多视图
https://github.com/Kennyc1012/MultiStateView

22.模块间通信https://github.com/alibaba/ARouter

//所用到的模块都得添加
// You can refer to the wording in the "module-kotlin" module
apply plugin: 'kotlin-kapt'

kapt {
    arguments {
        arg("AROUTER_MODULE_NAME", project.getName())
    }
}

dependencies {
    compile 'com.alibaba:arouter-api:x.x.x'
    kapt 'com.alibaba:arouter-compiler:x.x.x'
    ...
}


注入注解
ARoter.getInstance().inject(this)

可以跨模块接口调用

23.角标 类似于购物车的小角标
https://github.com/qstumn/BadgeView

24.rxBus
1>.定义事件
2>.发送时间
Bus.send
3>.监听时间observer
4>.销毁

override fun onDestroy() {
   super.onDestroy()
   Bus.unregister(this)
}
上一篇 下一篇

猜你喜欢

热点阅读