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
}
相关推荐
我爱挣钱我也要早睡!18 小时前
Java 复习笔记
java·开发语言·笔记
Yang-Never19 小时前
Kotlin协程 -> Job.join() 完整流程图与核心源码分析
android·开发语言·kotlin·android studio
XeonYu20 小时前
Kotlin 协程之 突破 Flow 限制:Channel 与 Flow 的结合之道
kotlin·coroutine·channelflow·callbackflow·receiveasflow·consumeasflow
TomCode先生21 小时前
c#动态树形表达式详解
开发语言·c#
高-老师21 小时前
基于R语言的物种气候生态位动态量化与分布特征模拟
开发语言·r语言·物种气候
大翻哥哥1 天前
Python 2025:量化金融与智能交易的新纪元
开发语言·python·金融
weixin_437830941 天前
使用冰狐智能辅助实现图形列表自动点击:OCR与HID技术详解
开发语言·javascript·ocr
鹿鹿学长1 天前
2025年全国大学生数学建模竞赛(C题) 建模解析|婴儿染色体数学建模|小鹿学长带队指引全代码文章与思路
c语言·开发语言·数学建模
zhousenshan1 天前
Python爬虫常用框架
开发语言·爬虫·python
DKPT1 天前
Java内存区域与内存溢出
java·开发语言·jvm·笔记·学习