Android Kotlin 实用扩展函数(持续更新)

在 Android Kotlin 实际的开发过程中,使用扩展函数能够使代码更符合人类的阅读规律和思维习惯,这是提高开发效率的一个核心的点。

本文会列举 Android 开发常用的扩展函数(持续更新)


WindowExt:

Kotlin 复制代码
// 设置状态栏字体颜色(白色)
fun Window.statusBarTextColorWhite() {
    WindowCompat.getInsetsController(this, decorView).isAppearanceLightStatusBars = false
}

// 设置状态栏字体颜色(黑色)
fun Window.statusBarTextColorBlack() {
    WindowCompat.getInsetsController(this, decorView).isAppearanceLightStatusBars = true
}

// 设置导航栏图标颜色(白色)
fun Window.navigationBarIconWhite() {
    WindowCompat.getInsetsController(this, decorView).isAppearanceLightNavigationBars = false
}

// 设置导航栏图标颜色(黑色)
fun Window.navigationBarIconBlack() {
    WindowCompat.getInsetsController(this, decorView).isAppearanceLightNavigationBars = true
}

ViewExt:

Kotlin 复制代码
fun View.visible() {
    if (visibility != View.VISIBLE) visibility = View.VISIBLE
}

fun View.gone() {
    if (visibility != View.GONE) visibility = View.GONE
}

fun View.invisible() {
    if (visibility != View.INVISIBLE) visibility = View.INVISIBLE
}

fun View.setTranslationXExt(offset: Float) {
    if (translationX != offset) translationX = offset
}

fun View.setTranslationYExt(offset: Float) {
    if (translationY != offset) translationY = offset
}

fun View.setPaddingExt(left: Int = -1, top: Int = -1, right: Int = -1, bottom: Int = -1, horizontal: Int = -1, vertical: Int = -1) {
    if (left != -1) {
        if (left != paddingLeft) setPadding(left, paddingTop, paddingRight, paddingBottom)
    }
    if (top != -1) {
        if (top != paddingTop) setPadding(paddingLeft, top, paddingRight, paddingBottom)
    }
    if (right != -1) {
        if (right != paddingRight) setPadding(paddingLeft, paddingTop, right, paddingBottom)
    }
    if (bottom != -1) {
        if (bottom != paddingBottom) setPadding(paddingLeft, paddingTop, paddingRight, bottom)
    }
    if (horizontal != -1) {
        if (horizontal != paddingLeft) setPadding(horizontal, paddingTop, paddingRight, paddingBottom)
        if (horizontal != paddingRight) setPadding(paddingLeft, paddingTop, horizontal, paddingBottom)
    }
    if (vertical != -1) {
        if (vertical != paddingTop) setPadding(paddingLeft, vertical, paddingRight, paddingBottom)
        if (vertical != paddingBottom) setPadding(paddingLeft, paddingTop, paddingRight, vertical)
    }
}

RvExt:

Kotlin 复制代码
// RecyclerView 中所有的 item 是否都可见
fun RecyclerView.isAllItemsVisible(): Boolean {
    val layoutManager = layoutManager as? LinearLayoutManager ?: return false
    val totalItemCount = layoutManager.itemCount
    if (totalItemCount == 0) {
        return true
    }
    val firstVisibleItem = layoutManager.findFirstCompletelyVisibleItemPosition()
    val lastVisibleItem = layoutManager.findLastCompletelyVisibleItemPosition()
    return firstVisibleItem == 0 && lastVisibleItem == totalItemCount - 1
}

// RecyclerView 最后一个 item 是否可见
fun RecyclerView.isEndItemVisible(): Boolean {
    val lastVisiblePosition = (layoutManager as LinearLayoutManager).findLastCompletelyVisibleItemPosition()
    return lastVisiblePosition == adapter!!.itemCount - 1
}

// implementation "io.github.cymchad:BaseRecyclerViewAdapterHelper4:4.1.4"
fun <T : Any, VH : RecyclerView.ViewHolder> BaseQuickAdapter<T, VH>.setItemClickLisExt(operation: (T, View, Int) -> Unit) {
    setOnItemClickListener { adt, view, position ->
        val item = adt.getItem(position) ?: return@setOnItemClickListener
        operation.invoke(item, view, position)
    }
}

// implementation "io.github.cymchad:BaseRecyclerViewAdapterHelper4:4.1.4"
fun <T : Any, VH : RecyclerView.ViewHolder> BaseQuickAdapter<T, VH>.addItemChildClickLisExt(viewId: Int, operation: (T, View, Int) -> Unit) {
    addOnItemChildClickListener(viewId) { adt, view, position ->
        val item = adt.getItem(position) ?: return@addOnItemChildClickListener
        operation.invoke(item, view, position)
    }
}

// implementation "io.github.cymchad:BaseRecyclerViewAdapterHelper4:4.1.4"
fun <T : Any, VH : RecyclerView.ViewHolder> BaseQuickAdapter<T, VH>.addItemChildLongClickLisExt(viewId: Int, operation: (T, View, Int) -> Unit) {
    addOnItemChildLongClickListener(viewId) { adt, view, position ->
        val item = adt.getItem(position) ?: return@addOnItemChildLongClickListener true
        operation.invoke(item, view, position)
        true
    }
}

SystemUIExt:

Kotlin 复制代码
// 沉浸式后需要填充 状态栏高度 的 view
fun initStateBar(view: View) {
    val layoutParams = view.layoutParams
    layoutParams.height = statusBarHeightPx
    view.layoutParams = layoutParams
}

// 沉浸式后需要填充 导航栏高度 的 view
fun initNavigationBar(view: View) {
    val layoutParams = view.layoutParams
    layoutParams.height = navigationBarHeightPx
    view.layoutParams = layoutParams
}

// 状态栏的像素高度
val statusBarHeightPx by lazy {
    val context = AppContext
    var heightPx = 0
    val resourceId = context.resources.getIdentifier("status_bar_height", "dimen", "android")
    if (resourceId > 0) {
        heightPx = context.resources.getDimensionPixelSize(resourceId)
    }
    heightPx
}

// 导航栏的像素高度
val navigationBarHeightPx by lazy {
    val context = AppContext
    var heightPx = 0
    val id = context.resources.getIdentifier("config_showNavigationBar", "bool", "android")
    if (id != 0) {
        val resourceId = context.resources.getIdentifier("navigation_bar_height", "dimen", "android")
        if (resourceId > 0) {
            heightPx = context.resources.getDimensionPixelSize(resourceId)
        }
    }
    heightPx
}
相关推荐
dtq042421 分钟前
C语言-结构体详解
c语言·开发语言·学习
梅雅达编程笔记33 分钟前
编程启蒙|Scratch 转 Python 系列第9天:字典/哈希表积木双向对照(AI大模型参数配置表实战)
开发语言·人工智能·python·numpy·pandas
持力行34 分钟前
C++与Java变量声明、定义及内存分配的核心区别
java·开发语言·c++
jinyishu_2 小时前
C++ 继承全解:从基础到高级特性
开发语言·c++
AFinalStone3 小时前
Android 7系统网络(八)应用API层—ConnectivityManager使用与实战调试
android·网络
plainGeekDev3 小时前
kapt 替换为 KSP
android·java·kotlin
蜡台3 小时前
通过Gradle脚本声明更改Java变量
android·java·开发语言·python·kotlin·gradle·groovy
小喻同学i4 小时前
std::thread函数参数传递
开发语言·c++
Ulyanov5 小时前
Python实现6-DOF刚体仿真器(下)——环境扰动与控制闭环
开发语言·python·算法·系统仿真·雷达电子对抗·导引头
分布式存储与RustFS5 小时前
RustFS Beta.10 性能解读:PUT 全面反超 MinIO,Rust 重写对象存储成了?
开发语言·后端·rust