From 814f1a3d5d375d58af9d3f687fbb64eae2ab3122 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=87=E8=B6=85?= <79556355@qq.com> Date: Thu, 26 Sep 2019 16:02:23 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=20update=20=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E4=B8=BA=E7=A9=BA=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/ms/banner/Banner.java | 30 ++++++++----------- 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/LibBanner/src/main/java/com/ms/banner/Banner.java b/LibBanner/src/main/java/com/ms/banner/Banner.java index 8bcd98c..bdcae64 100644 --- a/LibBanner/src/main/java/com/ms/banner/Banner.java +++ b/LibBanner/src/main/java/com/ms/banner/Banner.java @@ -56,6 +56,7 @@ public class Banner extends FrameLayout implements ViewPager.OnPageChangeListene private int titleTextSize; private int count = 0; private int currentItem = -1; + private int mCurrentPage = 0; private int gravity = -1; private int lastPosition; private List titles; @@ -238,21 +239,13 @@ public Banner setViewPagerIsScroll(boolean isScroll) { } public Banner setCurrentPage(@IntRange(from = 0) int page) { - if (count == 0) - return this; - if (page > count) { - throw new RuntimeException("[Banner] --> The current page is out of range"); - } - if (isLoop) { - this.currentItem = NUM / 2 - ((NUM / 2) % count) + 1 + page; - } else { - this.currentItem = page; - } + mCurrentPage = page; return this; } public Banner setPages(List datas, BannerViewHolder creator) { - this.mDatas = datas; + this.mDatas.clear(); + this.mDatas.addAll(datas); this.creator = creator; this.count = datas.size(); return this; @@ -272,17 +265,15 @@ public void update(List imageUrls) { if (imageUrls == null) { imageUrls = new ArrayList<>(); } + this.mDatas.clear(); + this.indicatorImages.clear(); if (imageUrls.size() == 0) { bannerDefaultImage.setVisibility(VISIBLE); - this.mDatas.clear(); - this.indicatorImages.clear(); this.count = 0; if (adapter != null) { adapter.notifyDataSetChanged(); } } else { - this.mDatas.clear(); - this.indicatorImages.clear(); this.mDatas.addAll(imageUrls); this.count = this.mDatas.size(); setOffscreenPageLimit(imageUrls.size()); @@ -448,13 +439,16 @@ else if (bannerStyle == BannerConfig.CUSTOM_INDICATOR) private void setData() { if (isLoop) { - //currentItem = 1; - if (currentItem == -1) { + if (mCurrentPage > 0 && mCurrentPage < count) { + this.currentItem = NUM / 2 - ((NUM / 2) % count) + 1 + mCurrentPage; + } else { currentItem = NUM / 2 - ((NUM / 2) % count) + 1; } lastPosition = 1; } else { - if (currentItem == -1) { + if (mCurrentPage > 0 && mCurrentPage < count) { + this.currentItem = mCurrentPage; + } else { currentItem = 0; } lastPosition = 0;