Skip to content

6.常用功能封装

鸡你太美 edited this page Nov 4, 2025 · 1 revision

1️⃣ 快速构建RecycleView与刷新控件 AdapterExt

1.1 SmartRefresh

mBind.refresh.refresh {
    //刷新
}.loadMore {
    //加载更多
}

1.2 Rv初始化 用的BRV库的方案

mBind.rv.vertical().divider {
    orientation = DividerOrientation.VERTICAL //分割线方向
    includeVisible= true //首尾有分割线
    setDivider(8, true) //分割线高度
}

1.3 分页加载

分页列表一般后端都会返回一个分页基类,你需要继承框架的BasePage类,并实现对应方法: 以玩安卓分页列表返回示例:

data class ApiPagerResponse<T>(
    var datas: ArrayList<T>,
    var curPage: Int,
    var offset: Int,
    var over: Boolean,
    var pageCount: Int,
    var size: Int,
    var total: Int
) : BasePage<T>() {
    override fun getPageData() = datas
    override fun isRefresh() = offset == 0
    override fun isEmpty() = datas.isEmpty()
    override fun hasMore() = !over
}

当我们请求到数据时,只需调用loadListSuccessloadListError方法即可使用框架封装好的分页与页面展示逻辑: 内置功能逻辑为:

  • 如果是刷新数据,切刷新的数据为空,既第一页没有数据,那么页面自动展示为空布局
  • 如果是第一页有数据,那么设置最新数据替换并关闭SmartRefresh刷新
  • 如果是加载更多数据,那么累加数据
  • 如果还有下一页数据 那么设置 SmartRefresh还可以加载更多数据
  • 如果没有更多数据了,设置 smartRefreshLayout 加载完毕 没有更多数据 使用示例:
provideRequest(isRefresh, loadingXml).obs(viewLifecycleOwner)  {
    onSuccess { pagerResponse -> 
        loadListSuccess(pagerResponse , mBind.rv.bindingAdapter,mBind.refresh,this@BaseArticleListFragment)
    }
    onError { statusEntity ->
        loadListError(statusEntity , mBind.refresh)
    }
}

2️⃣ 自动本地缓存 Cache

一个基于 MMKV 的缓存属性委托,作用是让你在声明变量时,自动完成数据的存储和读取。

  • 自动使用变量名作为缓存 key,不需要手动指定。
  • 支持常用类型:Boolean、Int、Long、Float、Double、String、ByteArray、Set、Parcelable。
  • 读取变量时自动从 MMKV 获取,赋值变量时自动写入 MMKV。
  • 让缓存操作像普通变量一样简洁,无需每次调用 mmkv.encode/decode。

使用示例:

var isLogin by Cache(false) //boolean
var userName by Cache("") //String
var tags by Cache(setOf()) //Set<String>
var user by Cache(User(0, "张三"))  // Parcelable 对象
var userCache : UserInfo? by cacheNullable() //对象默认值为null

3️⃣ 图片加载 ImageConfig

使用示例:

// 普通加载
imageView.load("url")
// 圆形加载:内部使用 CircleCrop()
imageView.loadCircle("url")                       
// 圆角加载
imageView.loadRound("url")

4️⃣ Json解析 GsonExt

使用示例:

// json数据转换成实体类
val user = json.toEntity<UserInfo>()
// json数据转换成实体类集合
val userList = json.toArrayEntity<UserInfo>()
// 对象转成json字符串
user.toJsonStr()

5️⃣ Toast,Log

使用示例:

"xxx".toast()
"xxx".logD()
"xxx".logE()

6️⃣ 启动优化 InitTaskManager

使用示例:

class App : Application() {
    override fun onCreate() {
        super.onCreate()
        if (isMainProcess()){
            // 只在主进程初始化 SDK
            JetpackMvvm.init(this)
            //启动初始化任务
            InitTaskManager
                .register(NetTask())
                .register(UtilTask())
                .register(WidgetTask())
                .execute(this)
        }
    }
}

7️⃣ 页面跳转bundle接收数据 Bundle

使用示例:

val article: ArticleResponse? by bundle()
val shareId by bundle("")

8️⃣ 跳转 Activity ActivityMessenger

使用示例:

//不携带参数
openActivity<TestActivity>()
//携带参数
openActivity<TestActivity>("Key" to "Value")

9️⃣ 快速拿到Activity栈 ActivityMangerExt

使用示例:

//关闭栈里面的任意一个Activity
finishActivityByClass(LoginActivity::class.java)
//关闭所有的Activity 全部出栈
finishAllActivity()
//拿app当前显示的Activity
currentActivity

还有很多扩展函数,这里就不一一写了,可查看 ext