diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..103e00c --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,32 @@ + + + + \ No newline at end of file diff --git a/banner/build.gradle b/banner/build.gradle index 1ae97d0..758a685 100644 --- a/banner/build.gradle +++ b/banner/build.gradle @@ -17,7 +17,7 @@ afterEvaluate { // You can then customize attributes of the publication as shown below. groupId = 'com.zj.banner' artifactId = 'banner' - version = '2.5.2' + version = '2.6.0' } // // Creates a Maven publication called “debug”. // debug(MavenPublication) { diff --git a/banner/src/main/java/com/zj/banner/BannerPager.kt b/banner/src/main/java/com/zj/banner/BannerPager.kt index 8a53a1f..8ef1eff 100644 --- a/banner/src/main/java/com/zj/banner/BannerPager.kt +++ b/banner/src/main/java/com/zj/banner/BannerPager.kt @@ -28,6 +28,7 @@ import java.util.* import kotlin.math.absoluteValue private const val TAG = "BannerPager" +private const val FAKE_BANNER_SIZE = 100 /** * 新增一个 Banner,最简单的情况下只需传入数据即可,如果需要更多样式请查看下面参数。 @@ -51,25 +52,29 @@ fun BannerPager( if (items.isEmpty()) { throw NullPointerException("items is not null") } - + val size = items.size val pagerState = rememberPagerState( initialPage = 0, initialPageOffsetFraction = 0f ) { - items.size + FAKE_BANNER_SIZE } if (config.repeat) { StartBanner(pagerState, config.intervalTime) } + val coroutineScope = rememberCoroutineScope() Box(modifier = modifier.height(config.bannerHeight)) { HorizontalPager( modifier = Modifier, state = pagerState, - key = { items[it].data?:it }, + key = { + it + }, pageContent = { page -> - val item = items[page] + val p = page % size + val item = items[p] BannerCard( bean = item, @@ -98,6 +103,20 @@ fun BannerPager( stop = 1f, fraction = 1f - pageOffset.coerceIn(0f, 1f) ) + + var position: Int = pagerState.currentPage + Log.d(TAG, "finish update before, position=$position") + if (position == 0) { + position = size + coroutineScope.launch { + pagerState.scrollToPage(position) + } + } else if (position == FAKE_BANNER_SIZE - 1) { + position = size - 1 + coroutineScope.launch { + pagerState.scrollToPage(position) + } + } } .fillMaxSize() .padding(config.bannerImagePadding), @@ -113,7 +132,7 @@ fun BannerPager( if (indicatorIsVertical) { VerticalPagerIndicator( pagerState = pagerState, - pageCount = items.size, + pageCount = size, modifier = Modifier .align(indicatorGravity) .padding(16.dp), @@ -121,7 +140,7 @@ fun BannerPager( } else { HorizontalPagerIndicator( pagerState = pagerState, - pageCount = items.size, + pageCount = size, modifier = Modifier .align(indicatorGravity) .padding(16.dp), diff --git a/banner/src/main/java/com/zj/banner/utils/PagerIndicator.kt b/banner/src/main/java/com/zj/banner/utils/PagerIndicator.kt index 6a7d90f..af351bb 100644 --- a/banner/src/main/java/com/zj/banner/utils/PagerIndicator.kt +++ b/banner/src/main/java/com/zj/banner/utils/PagerIndicator.kt @@ -127,9 +127,11 @@ private fun HorizontalPagerIndicator( Box( Modifier .offset { - val position = pageIndexMapping(pagerState.currentPage) + val currentPage = pagerState.currentPage + val p = currentPage % pageCount + val position = pageIndexMapping(p) val offset = pagerState.currentPageOffset - val next = pageIndexMapping(pagerState.currentPage + offset.sign.toInt()) + val next = pageIndexMapping(p + offset.sign.toInt()) val scrollPosition = ((next - position) * offset.absoluteValue + position) .coerceIn( 0f, @@ -253,9 +255,11 @@ private fun VerticalPagerIndicator( Box( Modifier .offset { - val position = pageIndexMapping(pagerState.currentPage) + val currentPage = pagerState.currentPage + val p = currentPage % pageCount + val position = pageIndexMapping(p) val offset = pagerState.currentPageOffset - val next = pageIndexMapping(pagerState.currentPage + offset.sign.toInt()) + val next = pageIndexMapping(p + offset.sign.toInt()) val scrollPosition = ((next - position) * offset.absoluteValue + position) .coerceIn( 0f,