From 6d472c8407bfaf674498c3e1ddaed25f9cd7fa46 Mon Sep 17 00:00:00 2001 From: fccaikai <714564390@qq.com> Date: Mon, 31 Oct 2016 21:50:28 +0800 Subject: [PATCH] =?UTF-8?q?1.fixBug=EF=BC=9A=E5=9B=BE=E7=89=87=E4=B8=8E?= =?UTF-8?q?=E6=A0=87=E9=A2=98=E5=AF=B9=E4=B8=8D=E4=B8=8A=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=202.fixBug=EF=BC=9A=E5=88=B7=E6=96=B0=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E6=97=B6=EF=BC=8C=E5=B0=8F=E5=9C=86=E7=82=B9=E4=BC=9A?= =?UTF-8?q?=E4=B8=8D=E6=96=AD=E5=A2=9E=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../view/AutoScrollViewPager.java | 13 +++++++ .../view/AutoViewPager.java | 12 ++++++ .../view/BaseViewPagerAdapter.java | 37 ++++++++++++------- 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/autoScrollViewPager/src/main/java/com/kcode/autoscrollviewpager/view/AutoScrollViewPager.java b/autoScrollViewPager/src/main/java/com/kcode/autoscrollviewpager/view/AutoScrollViewPager.java index 770b7f6..23151a5 100644 --- a/autoScrollViewPager/src/main/java/com/kcode/autoscrollviewpager/view/AutoScrollViewPager.java +++ b/autoScrollViewPager/src/main/java/com/kcode/autoscrollviewpager/view/AutoScrollViewPager.java @@ -103,6 +103,7 @@ public void setAdapter(BaseViewPagerAdapter adapter) { public void initPointView(int size) { + layout.removeAllViews(); for (int i = 0; i < size; i++) { ImageView imageView = new ImageView(mContext); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(20, 20); @@ -138,4 +139,16 @@ public void onDestroy() { } } + public void onResume(){ + if (mViewPager != null && !mViewPager.isStart()) { + mViewPager.start(); + } + } + + public void onPause(){ + if (mViewPager != null) { + mViewPager.onStop(); + } + } + } diff --git a/autoScrollViewPager/src/main/java/com/kcode/autoscrollviewpager/view/AutoViewPager.java b/autoScrollViewPager/src/main/java/com/kcode/autoscrollviewpager/view/AutoViewPager.java index 7cc3726..f5f8620 100644 --- a/autoScrollViewPager/src/main/java/com/kcode/autoscrollviewpager/view/AutoViewPager.java +++ b/autoScrollViewPager/src/main/java/com/kcode/autoscrollviewpager/view/AutoViewPager.java @@ -23,6 +23,11 @@ public class AutoViewPager extends ViewPager { private Timer mTimer; private AutoTask mTask; + private boolean isStart; + + public boolean isStart() { + return isStart; + } public AutoViewPager(Context context) { super(context); @@ -37,9 +42,14 @@ public void init(AutoViewPager viewPager,BaseViewPagerAdapter adapter){ } public void start(){ + + if (((BaseViewPagerAdapter)getAdapter()).getRealCount() == 0) { + return; + } //先停止 onStop(); + isStart = true; if (mTimer == null) { mTimer = new Timer(); } @@ -98,6 +108,8 @@ public void onStop(){ mTimer.cancel(); mTimer = null; } + + isStart = false; } public void onDestroy(){ diff --git a/autoScrollViewPager/src/main/java/com/kcode/autoscrollviewpager/view/BaseViewPagerAdapter.java b/autoScrollViewPager/src/main/java/com/kcode/autoscrollviewpager/view/BaseViewPagerAdapter.java index 3c62e1b..179b2db 100644 --- a/autoScrollViewPager/src/main/java/com/kcode/autoscrollviewpager/view/BaseViewPagerAdapter.java +++ b/autoScrollViewPager/src/main/java/com/kcode/autoscrollviewpager/view/BaseViewPagerAdapter.java @@ -64,8 +64,11 @@ public void init(AutoViewPager viewPager,BaseViewPagerAdapter adapter){ int position = Integer.MAX_VALUE/2 - (Integer.MAX_VALUE/2) % getRealCount(); mView.setCurrentItem(position); - mView.start(); - mView.updatePointView(getRealCount()); + if (!mView.isStart()) { + mView.start(); + mView.updatePointView(getRealCount()); + } + } public void setListener(OnAutoViewPagerItemClickListener listener) { @@ -102,8 +105,11 @@ public void init(List list){ notifyDataSetChanged(); - mView.start(); - mView.updatePointView(getRealCount()); + if (!mView.isStart()) { + mView.start(); + mView.updatePointView(getRealCount()); + } + } @@ -125,6 +131,19 @@ public void destroyItem(ViewGroup container, int position, Object object) { public Object instantiateItem(ViewGroup container, final int position) { ImageView view = (ImageView) LayoutInflater.from(mContext) .inflate(R.layout.imageview,container,false); + loadImage(view,position, data.get(position % getRealCount())); + container.addView(view); + + //设置标题(不知道为什么标题跟图片对不上,所以做了如下处理,有大神看到帮忙看看。。。) + if (mView.getSubTitle() != null){ + if (position == 0){ + setSubTitle(mView.getSubTitle(),position,data.get((getRealCount() - 1))); + }else { + setSubTitle(mView.getSubTitle(),position,data.get((position - 1) % getRealCount())); + } + + } + view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -134,16 +153,6 @@ public void onClick(View view) { } }); - loadImage(view,position, data.get(position % getRealCount())); - container.addView(view); - - //设置标题 - if (mView.getSubTitle() != null){ - setSubTitle(mView.getSubTitle(),position,data.get(position % getRealCount())); - }else { - - } - return view; }