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
}
相关推荐
FakeOccupational8 小时前
电路笔记(信号):网线能传多少米?网线信号传输距离
开发语言·笔记·php
李宥小哥8 小时前
Redis10-原理-网络模型
开发语言·网络·php
利刃大大8 小时前
【c++中间件】语音识别SDK && 二次封装
开发语言·c++·中间件·语音识别
浩浩的代码花园8 小时前
自研端侧推理模型实测效果展示
android·深度学习·计算机视觉·端智能
同学小张13 小时前
【端侧AI 与 C++】1. llama.cpp源码编译与本地运行
开发语言·c++·aigc·llama·agi·ai-native
踢球的打工仔14 小时前
PHP面向对象(7)
android·开发语言·php
安卓理事人14 小时前
安卓socket
android
汤姆yu16 小时前
基于python的外卖配送及数据分析系统
开发语言·python·外卖分析
Yue丶越16 小时前
【C语言】字符函数和字符串函数
c语言·开发语言·算法
翔云 OCR API17 小时前
人脸识别API开发者对接代码示例
开发语言·人工智能·python·计算机视觉·ocr