Skip to content

Latest commit

 

History

History
43 lines (32 loc) · 1.35 KB

File metadata and controls

43 lines (32 loc) · 1.35 KB

View AttachToWindow CoroutineScope

jitpack GitHub license

参考AndroidX-KTX中的lifecycleScope,将CoroutineScope绑定到ViewattachToWindow ~ detachedFromWindow期间范围。

Rxjava版本请查看:https://github.com/liu-wanshun/AndroidDisposable

添加依赖

  1. 添加jitpack仓库
allprojects {
    repositories {
        maven { url "https://jitpack.io" }
    }
}
  1. 添加view-attach-window-coroutine-scope 依赖最新版(将Tag替换为jitpack 后面的数字)
dependencies {
    implementation "com.github.liu-wanshun:view-attach-window-coroutine-scope:Tag"
}

用法

val view: View = findViewById<View>(R.id.test)
// 注意:
// 仅在attachToWindow ~ detachedFromWindow 范围期间获取的attachWindowScope可以执行
// 在onDetachedFromWindow后cancel
view.attachWindowScope.launch {

}
// 实际常在自定义View内部使用,一般不在View外部使用