From f8851b0795f47d6124daad4876618efbf561a280 Mon Sep 17 00:00:00 2001 From: zhujiang2 Date: Tue, 30 Mar 2021 10:27:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20BannerViewModel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/com/zj/test/BannerTest.kt | 72 +++++++++---------- .../java/com/zj/banner/BannerViewModel.kt | 17 ++--- .../src/main/java/com/zj/banner/ui/Pager.kt | 14 +++- 3 files changed, 55 insertions(+), 48 deletions(-) diff --git a/app/src/main/java/com/zj/test/BannerTest.kt b/app/src/main/java/com/zj/test/BannerTest.kt index 7d0bd10..e430487 100644 --- a/app/src/main/java/com/zj/test/BannerTest.kt +++ b/app/src/main/java/com/zj/test/BannerTest.kt @@ -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( @@ -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() +// } } } \ No newline at end of file diff --git a/banner/src/main/java/com/zj/banner/BannerViewModel.kt b/banner/src/main/java/com/zj/banner/BannerViewModel.kt index fc9e321..fb5f34c 100644 --- a/banner/src/main/java/com/zj/banner/BannerViewModel.kt +++ b/banner/src/main/java/com/zj/banner/BannerViewModel.kt @@ -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() } } } @@ -36,6 +32,7 @@ class BannerViewModel : ViewModel() { override fun onCleared() { super.onCleared() timer?.cancel() + mTimerTask?.cancel() } } \ No newline at end of file diff --git a/banner/src/main/java/com/zj/banner/ui/Pager.kt b/banner/src/main/java/com/zj/banner/ui/Pager.kt index c3f468d..7ddd3a7 100644 --- a/banner/src/main/java/com/zj/banner/ui/Pager.kt +++ b/banner/src/main/java/com/zj/banner/ui/Pager.kt @@ -1,5 +1,3 @@ -@file:Suppress("unused") - package com.zj.banner.ui import androidx.compose.animation.core.Animatable @@ -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)