From bb6f891976382e44cc5d77f80c9ca78eab7b9f25 Mon Sep 17 00:00:00 2001 From: lightSky <595806797@qq.com> Date: Mon, 10 Aug 2015 19:37:59 +0800 Subject: [PATCH 1/3] fix bugs and support vertical scroll --- demo/build.gradle | 2 +- demo/demo.iml | 2 +- .../DefaultCircleIndicatorActivity.java | 29 +++++++++++++++---- library/build.gradle | 8 ++--- library/library.iml | 6 ++-- .../InfiniteIndicatorLayout.java | 15 +++++----- .../indicator/AnimIndicator.java | 20 ++++++++----- .../indicator/RecyleAdapter.java | 1 - 8 files changed, 53 insertions(+), 30 deletions(-) diff --git a/demo/build.gradle b/demo/build.gradle index c0ffa5a..ca28d40 100644 --- a/demo/build.gradle +++ b/demo/build.gradle @@ -6,7 +6,7 @@ android { defaultConfig { applicationId "cn.lightsky.customeviewdemo" - minSdkVersion 11 + minSdkVersion 8 targetSdkVersion 20 versionCode 1 versionName "1.0" diff --git a/demo/demo.iml b/demo/demo.iml index c01c39d..8712abb 100644 --- a/demo/demo.iml +++ b/demo/demo.iml @@ -89,8 +89,8 @@ - + \ No newline at end of file diff --git a/demo/src/main/java/cn/lightsky/infiniteindicator/DefaultCircleIndicatorActivity.java b/demo/src/main/java/cn/lightsky/infiniteindicator/DefaultCircleIndicatorActivity.java index 4484dd4..8348752 100644 --- a/demo/src/main/java/cn/lightsky/infiniteindicator/DefaultCircleIndicatorActivity.java +++ b/demo/src/main/java/cn/lightsky/infiniteindicator/DefaultCircleIndicatorActivity.java @@ -1,20 +1,21 @@ package cn.lightsky.infiniteindicator; -import android.content.Intent; import android.os.Bundle; import android.support.v4.app.FragmentActivity; +import android.support.v4.view.ViewPager; +import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; +import java.util.ArrayList; +import java.util.HashMap; + import cn.light.sky.infiniteindicatordemo.R; import cn.lightsky.infiniteindicator.indicator.CircleIndicator; import cn.lightsky.infiniteindicator.slideview.BaseSliderView; import cn.lightsky.infiniteindicator.slideview.DefaultSliderView; -import java.util.ArrayList; -import java.util.HashMap; - public class DefaultCircleIndicatorActivity extends FragmentActivity implements BaseSliderView.OnSliderClickListener{ private InfiniteIndicatorLayout mCustoemIndicatorLayout; private ArrayList viewInfos; @@ -61,12 +62,29 @@ private void testCircleIndicator() { textSliderView .image(url_maps.get(name)) .setScaleType(BaseSliderView.ScaleType.Fit) + .showImageResForEmpty(R.drawable.ic_launcher) .setOnSliderClickListener(this); textSliderView.getBundle() - .putString("extra",name); + .putString("extra", name); mDefaultIndicator.addSlider(textSliderView); } mDefaultIndicator.setIndicatorPosition(InfiniteIndicatorLayout.IndicatorPosition.Center_Bottom); + mDefaultIndicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { + @Override + public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { + Log.i("TAG","onPage Scrolled ......."); + } + + @Override + public void onPageSelected(int position) { + Log.i("TAG","onPage onPageSelected ......."); + } + + @Override + public void onPageScrollStateChanged(int state) { + Log.i("TAG","onPage onPageScrollStateChanged ......."); + } + }); } private void testCustomeCircleIndicator() { @@ -76,6 +94,7 @@ private void testCustomeCircleIndicator() { textSliderView .image(url_maps.get(name)) .setScaleType(BaseSliderView.ScaleType.Fit) + .showImageResForError(R.drawable.ic_launcher) .setOnSliderClickListener(this); textSliderView.getBundle() .putString("extra",name); diff --git a/library/build.gradle b/library/build.gradle index bc26f9a..1c7e7ab 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -2,7 +2,7 @@ apply plugin: 'com.android.library' apply plugin: 'com.github.dcendents.android-maven' apply plugin: 'com.jfrog.bintray' -version = "1.0.1" +version = "1.0.3" android { compileSdkVersion 21 @@ -10,10 +10,10 @@ android { defaultConfig { resourcePrefix "lightsky" - minSdkVersion 11 + minSdkVersion 8 targetSdkVersion 20 - versionCode 1 - versionName "1.0" + versionCode 4 + versionName "1.0.3" } buildTypes { release { diff --git a/library/library.iml b/library/library.iml index d3c4b1e..4294a46 100644 --- a/library/library.iml +++ b/library/library.iml @@ -1,5 +1,5 @@ - + @@ -84,14 +84,16 @@ + + - + \ No newline at end of file diff --git a/library/src/main/java/cn/lightsky/infiniteindicator/InfiniteIndicatorLayout.java b/library/src/main/java/cn/lightsky/infiniteindicator/InfiniteIndicatorLayout.java index 5747b1e..8930a43 100644 --- a/library/src/main/java/cn/lightsky/infiniteindicator/InfiniteIndicatorLayout.java +++ b/library/src/main/java/cn/lightsky/infiniteindicator/InfiniteIndicatorLayout.java @@ -73,7 +73,6 @@ public class InfiniteIndicatorLayout extends RelativeLayout implements Recycling // private boolean isBorderAnimation = true; public static final int MSG_WHAT = 0; - private Handler handler; private boolean isAutoScroll = false; private boolean isStopByTouch = false; private float touchX = 0f, downX = 0f; @@ -118,7 +117,6 @@ else if(indicatorType == 1) mRecyleAdapter = new RecyleAdapter(mContext); mRecyleAdapter.setDataChangeListener(this); mViewPager.setAdapter(mRecyleAdapter); - handler = new ScrollHandler(); setViewPagerScroller(); } @@ -263,8 +261,6 @@ public boolean dispatchTouchEvent(MotionEvent ev) { return super.dispatchTouchEvent(ev); } } - getParent().requestDisallowInterceptTouchEvent(true); - return super.dispatchTouchEvent(ev); } @@ -274,12 +270,10 @@ public void notifyDataChange() { mIndicator.notifyDataSetChanged(); } - private class ScrollHandler extends Handler { - + private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); - switch (msg.what) { case MSG_WHAT: scrollOnce(); @@ -288,7 +282,7 @@ public void handleMessage(Message msg) { break; } } - } + }; /** * get auto scroll interval time in milliseconds, default is {@link #DEFAULT_INTERVAL} @@ -429,4 +423,9 @@ public void setCustomIndicator(PageIndicator indicator) { // startAutoScroll(); } + public void setOnPageChangeListener(ViewPager.OnPageChangeListener onPageChangeListener){ + if(onPageChangeListener!=null) + mIndicator.setOnPageChangeListener(onPageChangeListener); + } + } diff --git a/library/src/main/java/cn/lightsky/infiniteindicator/indicator/AnimIndicator.java b/library/src/main/java/cn/lightsky/infiniteindicator/indicator/AnimIndicator.java index 1532fe5..cb92b33 100644 --- a/library/src/main/java/cn/lightsky/infiniteindicator/indicator/AnimIndicator.java +++ b/library/src/main/java/cn/lightsky/infiniteindicator/indicator/AnimIndicator.java @@ -17,7 +17,7 @@ import static android.support.v4.view.ViewPager.OnPageChangeListener; -public class AnimIndicator extends LinearLayout implements PageIndicator{ +public class AnimIndicator extends LinearLayout implements PageIndicator { private final static int DEFAULT_INDICATOR_WIDTH = 5; @@ -124,7 +124,7 @@ public void notifyDataSetChanged() { @Override public void onPageScrolled(int position, float positionOffset, - int positionOffsetPixels) { + int positionOffsetPixels) { if (mViewPagerOnPageChangeListener != null) { mViewPagerOnPageChangeListener.onPageScrolled(position, positionOffset, positionOffsetPixels); @@ -137,12 +137,12 @@ public void onPageSelected(int position) { mViewPagerOnPageChangeListener.onPageSelected(position); } - if(getChildAt(((RecyleAdapter) mViewPager.getAdapter()).getPosition(mCurrentPage))==null) + if (getRealChildAt(mCurrentPage) == null) return; - mAnimationIn.setTarget(getChildAt(((RecyleAdapter) mViewPager.getAdapter()).getPosition(mCurrentPage))); + mAnimationIn.setTarget(getRealChildAt(mCurrentPage)); mAnimationIn.start(); - mAnimationOut.setTarget(getChildAt(((RecyleAdapter) mViewPager.getAdapter()).getPosition(position))); + mAnimationOut.setTarget(getRealChildAt(position)); mAnimationOut.start(); mCurrentPage = position; @@ -158,11 +158,11 @@ public void onPageScrollStateChanged(int state) { private void createIndicators(ViewPager viewPager) { removeAllViews(); - if(((RecyleAdapter)viewPager.getAdapter())==null) { + if (((RecyleAdapter) viewPager.getAdapter()) == null) { return; } - int count = ((RecyleAdapter)viewPager.getAdapter()).getRealCount(); + int count = ((RecyleAdapter) viewPager.getAdapter()).getRealCount(); if (count <= 1) { return; } @@ -180,10 +180,14 @@ private void createIndicators(ViewPager viewPager) { mAnimationOut.start(); } - mAnimationOut.setTarget(getChildAt(mCurrentPage)); + mAnimationOut.setTarget(getRealChildAt(mCurrentPage)); mAnimationOut.start(); } + private View getRealChildAt(int position) { + return getChildAt(((RecyleAdapter) mViewPager.getAdapter()).getPosition(position)); + } + private class ReverseInterpolator implements Interpolator { @Override public float getInterpolation(float value) { diff --git a/library/src/main/java/cn/lightsky/infiniteindicator/indicator/RecyleAdapter.java b/library/src/main/java/cn/lightsky/infiniteindicator/indicator/RecyleAdapter.java index ad08cce..e9ed6c6 100644 --- a/library/src/main/java/cn/lightsky/infiniteindicator/indicator/RecyleAdapter.java +++ b/library/src/main/java/cn/lightsky/infiniteindicator/indicator/RecyleAdapter.java @@ -71,7 +71,6 @@ public void removeSliderAt(int position) { public void removeAllSliders() { mSlederViews.clear(); - mDataChangeListener.notifyDataChange(); notifyDataSetChanged(); } From 91d3ca385c7ec8e0dd5c1a7b41a4309b65892b36 Mon Sep 17 00:00:00 2001 From: lightSky <595806797@qq.com> Date: Tue, 11 Aug 2015 15:56:55 +0800 Subject: [PATCH 2/3] optimize --- demo/build.gradle | 1 - demo/demo.iml | 2 +- library/library.iml | 4 ++-- .../infiniteindicator/indicator/AnimIndicator.java | 12 ++++++------ .../infiniteindicator/indicator/RecyleAdapter.java | 1 - 5 files changed, 9 insertions(+), 11 deletions(-) diff --git a/demo/build.gradle b/demo/build.gradle index ca28d40..e6f297b 100644 --- a/demo/build.gradle +++ b/demo/build.gradle @@ -25,6 +25,5 @@ android { } dependencies { -// compile fileTree(dir: 'libs', include: ['*.jar']) compile project(':library') } diff --git a/demo/demo.iml b/demo/demo.iml index 8712abb..c01c39d 100644 --- a/demo/demo.iml +++ b/demo/demo.iml @@ -89,8 +89,8 @@ - + \ No newline at end of file diff --git a/library/library.iml b/library/library.iml index 4294a46..96c943a 100644 --- a/library/library.iml +++ b/library/library.iml @@ -1,5 +1,5 @@ - + @@ -93,7 +93,7 @@ - + \ No newline at end of file diff --git a/library/src/main/java/cn/lightsky/infiniteindicator/indicator/AnimIndicator.java b/library/src/main/java/cn/lightsky/infiniteindicator/indicator/AnimIndicator.java index cb92b33..0099709 100644 --- a/library/src/main/java/cn/lightsky/infiniteindicator/indicator/AnimIndicator.java +++ b/library/src/main/java/cn/lightsky/infiniteindicator/indicator/AnimIndicator.java @@ -168,15 +168,15 @@ private void createIndicators(ViewPager viewPager) { } for (int i = 0; i < count; i++) { - View Indicator = new View(getContext()); - Indicator.setBackgroundResource(mIndicatorBackground); - addView(Indicator, mIndicatorWidth, mIndicatorHeight); - LayoutParams lp = (LayoutParams) Indicator.getLayoutParams(); + View indicator = new View(getContext()); + indicator.setBackgroundResource(mIndicatorBackground); + addView(indicator, mIndicatorWidth, mIndicatorHeight); + LayoutParams lp = (LayoutParams) indicator.getLayoutParams(); lp.leftMargin = mIndicatorMargin; lp.rightMargin = mIndicatorMargin; - Indicator.setLayoutParams(lp); + indicator.setLayoutParams(lp); - mAnimationOut.setTarget(Indicator); + mAnimationOut.setTarget(indicator); mAnimationOut.start(); } diff --git a/library/src/main/java/cn/lightsky/infiniteindicator/indicator/RecyleAdapter.java b/library/src/main/java/cn/lightsky/infiniteindicator/indicator/RecyleAdapter.java index e9ed6c6..ea2121d 100644 --- a/library/src/main/java/cn/lightsky/infiniteindicator/indicator/RecyleAdapter.java +++ b/library/src/main/java/cn/lightsky/infiniteindicator/indicator/RecyleAdapter.java @@ -51,7 +51,6 @@ public int getCount() { @Override public View getView(final int position, View convertView, ViewGroup container) { - BaseSliderView sliderView = ((BaseSliderView) mSlederViews.get(getPosition(position))); return ((BaseSliderView) mSlederViews.get(getPosition(position))).getView(); } From 0aeb786c2065fda035cbe14bd07cc8f02ddff923 Mon Sep 17 00:00:00 2001 From: lightSky <595806797@qq.com> Date: Tue, 11 Aug 2015 18:26:06 +0800 Subject: [PATCH 3/3] fix handler memory leak --- .../InfiniteIndicatorLayout.java | 40 +++++++++++++++---- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/library/src/main/java/cn/lightsky/infiniteindicator/InfiniteIndicatorLayout.java b/library/src/main/java/cn/lightsky/infiniteindicator/InfiniteIndicatorLayout.java index 8930a43..c0da50c 100644 --- a/library/src/main/java/cn/lightsky/infiniteindicator/InfiniteIndicatorLayout.java +++ b/library/src/main/java/cn/lightsky/infiniteindicator/InfiniteIndicatorLayout.java @@ -13,6 +13,7 @@ import android.view.animation.Interpolator; import android.widget.RelativeLayout; +import java.lang.ref.WeakReference; import java.lang.reflect.Field; import cn.lightsky.infiniteindicator.indicator.PageIndicator; @@ -26,6 +27,7 @@ * Thanks to: https://github.com/Trinea/android-auto-scroll-view-pager */ public class InfiniteIndicatorLayout extends RelativeLayout implements RecyclingPagerAdapter.DataChangeListener { + private final ScrollHandler handler; private PageIndicator mIndicator; private ViewPager mViewPager; private Context mContext; @@ -113,6 +115,7 @@ else if(indicatorType == 1) else LayoutInflater.from(context).inflate(R.layout.layout_anim_line_indicator, this, true); + handler = new ScrollHandler(this); mViewPager = (ViewPager) findViewById(R.id.view_pager); mRecyleAdapter = new RecyleAdapter(mContext); mRecyleAdapter.setDataChangeListener(this); @@ -180,6 +183,11 @@ private void sendScrollMessage(long delayTimeInMills) { handler.sendEmptyMessageDelayed(MSG_WHAT, delayTimeInMills); } + private void sendScrollMessage() { + /** remove messages before, keeps one message is running at most **/ + sendScrollMessage(interval); + } + /** * modify duration of ViewPager */ @@ -270,19 +278,30 @@ public void notifyDataChange() { mIndicator.notifyDataSetChanged(); } - private Handler handler = new Handler(){ + public static class ScrollHandler extends Handler{ + public WeakReference mLeakActivityRef; + + public ScrollHandler(InfiniteIndicatorLayout infiniteIndicatorLayout) { + mLeakActivityRef = new WeakReference(infiniteIndicatorLayout); + } + @Override public void handleMessage(Message msg) { super.handleMessage(msg); - switch (msg.what) { - case MSG_WHAT: - scrollOnce(); - sendScrollMessage(interval); - default: - break; + + InfiniteIndicatorLayout infiniteIndicatorLayout = mLeakActivityRef.get(); + if(infiniteIndicatorLayout != null){ + switch (msg.what) { + case MSG_WHAT: + infiniteIndicatorLayout.scrollOnce(); + infiniteIndicatorLayout.sendScrollMessage(); + default: + break; + } } } - }; + } + /** * get auto scroll interval time in milliseconds, default is {@link #DEFAULT_INTERVAL} @@ -428,4 +447,9 @@ public void setOnPageChangeListener(ViewPager.OnPageChangeListener onPageChangeL mIndicator.setOnPageChangeListener(onPageChangeListener); } + @Override + protected void onDetachedFromWindow() { + super.onDetachedFromWindow(); + handler.removeCallbacksAndMessages(null); + } }