Skip to content

Commit 6933baf

Browse files
committed
添加经典刷新自动加载示例(#24
1 parent 5f73d0a commit 6933baf

File tree

5 files changed

+153
-1
lines changed

5 files changed

+153
-1
lines changed

app/release/app-release.apk

4.58 KB
Binary file not shown.

app/src/main/java/com/king/ultraswiperefresh/app/navigation/NavGraph.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import androidx.navigation.NavController
44
import androidx.navigation.NavGraphBuilder
55
import androidx.navigation.compose.composable
66
import androidx.navigation.navigation
7+
import com.king.ultraswiperefresh.app.sample.ClassicRefreshAutoLoadSample
78
import com.king.ultraswiperefresh.app.sample.ClassicRefreshIndicatorSample
89
import com.king.ultraswiperefresh.app.sample.CustomLottieRefreshIndicatorSample
910
import com.king.ultraswiperefresh.app.sample.LottieRefreshIndicatorSample
@@ -34,6 +35,9 @@ fun NavGraphBuilder.noteNavGraph(navController: NavController) {
3435
composable(route = NavRoute.ClassicRefreshIndicatorSample.name) {
3536
ClassicRefreshIndicatorSample()
3637
}
38+
composable(route = NavRoute.ClassicRefreshAutoLoadSample.name) {
39+
ClassicRefreshAutoLoadSample()
40+
}
3741
composable(route = NavRoute.ProgressRefreshIndicatorSample.name) {
3842
ProgressRefreshIndicatorSample()
3943
}

app/src/main/java/com/king/ultraswiperefresh/app/navigation/NavRoute.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ enum class NavRoute {
1717

1818
ClassicRefreshIndicatorSample,
1919

20+
ClassicRefreshAutoLoadSample,
21+
2022
ProgressRefreshIndicatorSample,
2123

2224
LottieRefreshIndicatorSample,
@@ -26,4 +28,4 @@ enum class NavRoute {
2628
SwipeRefreshSample,
2729

2830
PullRefreshSample,
29-
}
31+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
}

app/src/main/java/com/king/ultraswiperefresh/app/sample/UltraSwipeRefreshSample.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ fun UltraSwipeRefreshSample(navController: NavController) {
6060
NavRoute.ClassicRefreshIndicatorSample,
6161
"经典刷新样式示例" to "使用NestedScrollMode.Translate;特点:平移; 即:Header或 Footer与内容一起滑动"
6262
)
63+
put(
64+
NavRoute.ClassicRefreshAutoLoadSample,
65+
"经典刷新自动加载示例" to "使用NestedScrollMode.Translate;特点:平移; 即:Header或 Footer与内容一起滑动,并可自动加载更多"
66+
)
6367
put(
6468
NavRoute.ProgressRefreshIndicatorSample,
6569
"进度条刷新样式示例" to "使用NestedScrollMode.FixedFront;特点:固定在前面;即:Header或 Footer和内容都固定,仅改变状态"

0 commit comments

Comments
 (0)