|
| 1 | +package com.king.ultraswiperefresh.app.sample |
| 2 | + |
| 3 | +import androidx.compose.foundation.background |
| 4 | +import androidx.compose.foundation.layout.Box |
| 5 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 6 | +import androidx.compose.foundation.layout.padding |
| 7 | +import androidx.compose.foundation.lazy.LazyColumn |
| 8 | +import androidx.compose.foundation.lazy.rememberLazyListState |
| 9 | +import androidx.compose.material3.HorizontalDivider |
| 10 | +import androidx.compose.material3.Text |
| 11 | +import androidx.compose.runtime.Composable |
| 12 | +import androidx.compose.runtime.LaunchedEffect |
| 13 | +import androidx.compose.runtime.getValue |
| 14 | +import androidx.compose.runtime.mutableIntStateOf |
| 15 | +import androidx.compose.runtime.mutableStateOf |
| 16 | +import androidx.compose.runtime.remember |
| 17 | +import androidx.compose.runtime.rememberCoroutineScope |
| 18 | +import androidx.compose.runtime.setValue |
| 19 | +import androidx.compose.runtime.snapshotFlow |
| 20 | +import androidx.compose.ui.Alignment |
| 21 | +import androidx.compose.ui.Modifier |
| 22 | +import androidx.compose.ui.graphics.Color |
| 23 | +import androidx.compose.ui.unit.dp |
| 24 | +import androidx.compose.ui.unit.sp |
| 25 | +import com.king.ultraswiperefresh.UltraSwipeRefresh |
| 26 | +import com.king.ultraswiperefresh.app.component.ColumnItem |
| 27 | +import com.king.ultraswiperefresh.indicator.classic.ClassicRefreshFooter |
| 28 | +import com.king.ultraswiperefresh.indicator.classic.ClassicRefreshHeader |
| 29 | +import com.king.ultraswiperefresh.rememberUltraSwipeRefreshState |
| 30 | +import kotlinx.coroutines.delay |
| 31 | +import kotlinx.coroutines.flow.distinctUntilChanged |
| 32 | +import kotlinx.coroutines.launch |
| 33 | + |
| 34 | +/** |
| 35 | + * 经典刷新自动加载示例 |
| 36 | + * |
| 37 | + * @author <a href="mailto:[email protected]">Jenly</a> |
| 38 | + * <p> |
| 39 | + * <a href="https://github.com/jenly1314">Follow me</a> |
| 40 | + */ |
| 41 | +@Composable |
| 42 | +fun ClassicRefreshAutoLoadSample() { |
| 43 | + |
| 44 | + val state = rememberUltraSwipeRefreshState() |
| 45 | + var itemCount by remember { mutableIntStateOf(20) } |
| 46 | + var hasMoreData by remember { mutableStateOf(true) } |
| 47 | + var autoLoading by remember { mutableStateOf(false) } |
| 48 | + val coroutineScope = rememberCoroutineScope() |
| 49 | + val lazyListState = rememberLazyListState() |
| 50 | + |
| 51 | + LaunchedEffect(Unit) { |
| 52 | + snapshotFlow { |
| 53 | + val lazyListItemInfo = lazyListState.layoutInfo.visibleItemsInfo.lastOrNull() |
| 54 | + // 此处的 lazyListItemInfo.index > itemCount - 5 只是举个触发自动加载的条件示例;想要什么时候触发自动加载,可结合item高度和偏移量自行决定。 |
| 55 | + lazyListItemInfo != null && lazyListItemInfo.index > itemCount - 5 |
| 56 | + }.distinctUntilChanged().collect { |
| 57 | + if (!autoLoading && it) { |
| 58 | + if (itemCount >= 60) { |
| 59 | + hasMoreData = false |
| 60 | + } else { |
| 61 | + autoLoading = true |
| 62 | + // TODO 自动加载更多的逻辑处理,此处的延时只是为了演示效果 |
| 63 | + delay(200) |
| 64 | + itemCount += 20 |
| 65 | + autoLoading = false |
| 66 | + |
| 67 | + state.isLoading = false |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + LaunchedEffect(state.isFinishing) { |
| 74 | + if (itemCount >= 60 && !state.isFinishing) { |
| 75 | + hasMoreData = false |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + UltraSwipeRefresh( |
| 80 | + state = state, |
| 81 | + onRefresh = { |
| 82 | + coroutineScope.launch { |
| 83 | + state.isRefreshing = true |
| 84 | + // TODO 刷新的逻辑处理,此处的延时只是为了演示效果 |
| 85 | + delay(2000) |
| 86 | + itemCount = 20 |
| 87 | + hasMoreData = true |
| 88 | + state.isRefreshing = false |
| 89 | + } |
| 90 | + }, |
| 91 | + onLoadMore = { |
| 92 | + coroutineScope.launch { |
| 93 | + state.isLoading = true |
| 94 | + if (!autoLoading) { // 当前非自动加载时,才去加载更多数据 |
| 95 | + // TODO 加载更多的逻辑处理,此处的延时只是为了演示效果 |
| 96 | + delay(2000) |
| 97 | + itemCount += 20 |
| 98 | + state.isLoading = false |
| 99 | + } |
| 100 | + } |
| 101 | + }, |
| 102 | + modifier = Modifier.background(color = Color(0x7FEEEEEE)), |
| 103 | + loadMoreTriggerRate = 0.01f, |
| 104 | + loadMoreEnabled = hasMoreData, |
| 105 | + headerIndicator = { |
| 106 | + ClassicRefreshHeader(it) |
| 107 | + }, |
| 108 | + footerIndicator = { |
| 109 | + ClassicRefreshFooter(it) |
| 110 | + } |
| 111 | + ) { |
| 112 | + LazyColumn( |
| 113 | + modifier = Modifier.background(color = Color.White), |
| 114 | + state = lazyListState, |
| 115 | + ) { |
| 116 | + repeat(itemCount) { |
| 117 | + item { |
| 118 | + val title = "UltraSwipeRefresh列表标题${it + 1}" |
| 119 | + val content = "UltraSwipeRefresh列表内容${it + 1}" |
| 120 | + ColumnItem(title = title, content = content) |
| 121 | + HorizontalDivider( |
| 122 | + modifier = Modifier.padding(horizontal = 16.dp), |
| 123 | + color = Color(0xFFF2F3F6) |
| 124 | + ) |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + if (!hasMoreData) { |
| 129 | + item { |
| 130 | + Box(modifier = Modifier.fillMaxWidth(), contentAlignment = Alignment.Center) { |
| 131 | + Text( |
| 132 | + text = "没有更多数据了", |
| 133 | + color = Color(0xFF999999), |
| 134 | + fontSize = 15.sp, |
| 135 | + modifier = Modifier.padding(vertical = 16.dp) |
| 136 | + ) |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments