summerain0的个人博客

  • 首页
  • 意见反馈
  • 友情链接
  • 申请友联
每个Bug都有解决之道
在修了在修了٩(๛ ˘ ³˘)۶
  1. 首页
  2. Compose
  3. 正文

Android Compose - 官方SwipeRefreshLayout(下拉刷新)

2021年11月6日 474点热度 4人点赞 0条评论
浏览量: 466
内容 隐藏目录
1) Demo
2) 类库引用
3) 用法

Demo

类库引用

Java
1
2
3
4
5
6
7
repositories {
    mavenCentral()
}
 
dependencies {
    implementation "com.google.accompanist:accompanist-swiperefresh:<version>"
}

用法

Kotlin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class MyViewModel : ViewModel() {
    private val _isRefreshing = MutableStateFlow(false)
 
    val isRefreshing: StateFlow<Boolean>
        get() = _isRefreshing.asStateFlow()
 
    fun refresh() {
        viewModelScope.launch {
            // 延迟2秒
            _isRefreshing.emit(true)
            delay(2000)
            _isRefreshing.emit(false)
        }
    }
}
 
@Composable
fun SwipeRefreshSample() {
    val viewModel: MyViewModel = viewModel()
    val isRefreshing by viewModel.isRefreshing.collectAsState()
 
    SwipeRefresh(
        state = rememberSwipeRefreshState(isRefreshing),
        onRefresh = { viewModel.refresh() },
    ) {
        LazyColumn {
            items(30) { index ->
                // item列表
            }
        }
    }
}
 

 

微海报分享
标签: Android 教程
最后更新:2022年3月3日

summerain0

这个人很懒,什么都没留下

打赏 点赞
< 上一篇
归档
  • 2021年11月
  • 2021年4月
  • 2021年3月
  • 2021年1月
  • 2020年10月
  • 2020年9月
分类
  • Android
  • Compose
  • Java
  • Material Design
  • 工具

COPYRIGHT © 2021 summerain0.club. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

闽ICP备19012197号-1