在进行Compose应用开发时,发现没有下拉刷新上拉加载,没有Android原生提供的SwipeRefreshLayout。

查看官方simple样例(https://github.com/android/compose-samples),在官方样例的基础进行简单封装,

编写的了ComposeRefreshLayout库(github:https://github.com/xiaoyu00/ComposeRefreshLayout)


ComposeRefreshLayout是一个简单易用的下拉刷新库,代码简单易用,可自定义刷新头与上拉加载View。

效果展示

说明

因刷新头与上拉加载View全部为自定义,比较灵活,所以本库里不包含默认刷新头与加载View,效果中的刷新头与加载View代码在app目录下的simple里

引入

repositories {

maven { url ‘https://jitpack.io’ }
}

dependencies {
  implementation ‘com.github.xiaoyu00:ComposeRefreshLayout:1.0.0’
}

下拉刷新

RefreshLayout(refreshingState = refreshing, onRefresh = {            refreshing = true            scope.launch {                delay(2000)                refreshing = false            }        }, refreshHeader = { offset, state ->           // RefreshDefaultHeader(state, offset)   // RefreshLottieHeader(state)           // 你的刷新Header(上面刷新头代码在app下simple里)
      ... }) { // 刷新内容 ... } }

上拉加载(与下拉刷新使用方法相同)

LoadMoreLayout(loadMoreState = isLoadMore, onLoadMore = {        isLoadMore = true        scope.launch {            delay(2000)            isLoadMore = false        }    }, loadMore = { offset, state ->        // LoadMoreDefaultView(state, isNoMore)(此代码在app下simple里)// 你的加载View
    ... }) {
    // 加载内容
    ...
}

组合使用

LoadMoreLayout(loadMoreState = isLoadMore, onLoadMore = {        isLoadMore = true        scope.launch {            delay(2000)            isLoadMore = false        }    }, loadMore = { offset, state ->        // LoadMoreDefaultView(state, isNoMore)(此代码在app下simple里)// 你的加载View
    ... }) { RefreshLayout(refreshingState = refreshing, onRefresh = { refreshing = true scope.launch { delay(2000) refreshing = false } }, refreshHeader = { offset, state -> //RefreshDefaultHeader(state, offset) (此刷新头代码在app下simple里) // 你的刷新Header
      ... }) { //内容
      ... } }