Skip to content

Commit

Permalink
优化 BannerViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
zhujiang2 committed Mar 30, 2021
1 parent 4543fad commit f8851b0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 48 deletions.
72 changes: 36 additions & 36 deletions app/src/main/java/com/zj/test/BannerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,26 @@ fun BannerTest() {
val scroller = rememberScrollState()
Column(modifier = Modifier.fillMaxSize().verticalScroll(scroller)) {

val items = arrayListOf(
BannerBean(
"https://wanandroid.com/blogimgs/8a0131ac-05b7-4b6c-a8d0-f438678834ba.png",
),
BannerBean(
"https://www.wanandroid.com/blogimgs/62c1bd68-b5f3-4a3c-a649-7ca8c7dfabe6.png",
),
BannerBean(
"https://www.wanandroid.com/blogimgs/50c115c2-cf6c-4802-aa7b-a4334de444cd.png",
),
BannerBean(
"https://www.wanandroid.com/blogimgs/90c6cc12-742e-4c9f-b318-b912f163b8d0.png",
),
)

BannerPager(
items = items,
) { item ->
Toast.makeText(context, "item:$item", Toast.LENGTH_SHORT).show()
}
// val items = arrayListOf(
// BannerBean(
// "https://wanandroid.com/blogimgs/8a0131ac-05b7-4b6c-a8d0-f438678834ba.png",
// ),
// BannerBean(
// "https://www.wanandroid.com/blogimgs/62c1bd68-b5f3-4a3c-a649-7ca8c7dfabe6.png",
// ),
// BannerBean(
// "https://www.wanandroid.com/blogimgs/50c115c2-cf6c-4802-aa7b-a4334de444cd.png",
// ),
// BannerBean(
// "https://www.wanandroid.com/blogimgs/90c6cc12-742e-4c9f-b318-b912f163b8d0.png",
// ),
// )
//
// BannerPager(
// items = items,
// ) { item ->
// Toast.makeText(context, "item:$item", Toast.LENGTH_SHORT).show()
// }


val items2 = arrayListOf(
Expand All @@ -65,22 +65,22 @@ fun BannerTest() {
}


BannerPager(
modifier = Modifier.padding(top = 10.dp),
items = items,
indicator = NumberIndicator()
) { item ->
Toast.makeText(context, "item:$item", Toast.LENGTH_SHORT).show()
}


BannerPager(
modifier = Modifier.padding(top = 10.dp),
items = items2,
indicator = NumberIndicator(gravity = BannerGravity.BottomLeft)
) { item ->
Toast.makeText(context, "item:$item", Toast.LENGTH_SHORT).show()
}
// BannerPager(
// modifier = Modifier.padding(top = 10.dp),
// items = items,
// indicator = NumberIndicator()
// ) { item ->
// Toast.makeText(context, "item:$item", Toast.LENGTH_SHORT).show()
// }
//
//
// BannerPager(
// modifier = Modifier.padding(top = 10.dp),
// items = items2,
// indicator = NumberIndicator(gravity = BannerGravity.BottomLeft)
// ) { item ->
// Toast.makeText(context, "item:$item", Toast.LENGTH_SHORT).show()
// }

}
}
17 changes: 7 additions & 10 deletions banner/src/main/java/com/zj/banner/BannerViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,19 @@ import java.util.*
class BannerViewModel : ViewModel() {

private var timer: Timer? = null
private var _isActive = false
private var mTimerTask: TimerTask? = null
private var isActive = false

fun startBanner(pagerState: PagerState, intervalTime: Long) {
if (_isActive) {
if (isActive) {
return
}
_isActive = true
isActive = true
timer = Timer()
val mTimerTask: TimerTask = object : TimerTask() {
mTimerTask = object : TimerTask() {
override fun run() {
viewModelScope.launch {
if (pagerState.currentPage == pagerState.maxPage) {
pagerState.currentPage = 0
} else {
pagerState.currentPage++
}
pagerState.fling(-1f)
pagerState.setNextPage()
}
}
}
Expand All @@ -36,6 +32,7 @@ class BannerViewModel : ViewModel() {
override fun onCleared() {
super.onCleared()
timer?.cancel()
mTimerTask?.cancel()
}

}
14 changes: 12 additions & 2 deletions banner/src/main/java/com/zj/banner/ui/Pager.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@file:Suppress("unused")

package com.zj.banner.ui

import androidx.compose.animation.core.Animatable
Expand Down Expand Up @@ -69,6 +67,18 @@ class PagerState(
selectPage()
}

/**
* 切换下一个页面
*/
suspend fun setNextPage() {
if (currentPage == maxPage) {
currentPage = 0
} else {
currentPage++
}
fling(-1f)
}

suspend fun selectPage() {
currentPage -= currentPageOffset.roundToInt()
snapToOffset(0f)
Expand Down

0 comments on commit f8851b0

Please sign in to comment.