diff --git a/README.md b/README.md index 49913f06..ca49b2eb 100644 --- a/README.md +++ b/README.md @@ -97,17 +97,19 @@ Simple steps, you can integrate **MagicIndicator**: magicIndicator.setNavigator(commonNavigator); ``` -5. bind **magicindicator** to ViewPager: +5. work with ViewPager: ``` - SimpleViewPagerDelegate.with(magicIndicator, mViewPager).delegate(); + ViewPagerHelper.bind(magicIndicator, mViewPager); ``` -6. work with Fragment Container(no ViewPager, switch Pages by hand): + **or** + + work with Fragment Container(switch Fragment by hide()、show()): ``` mFramentContainerHelper = new FragmentContainerHelper(magicIndicator); ... - mFragmentContainerHelper.handlePageSelected(pageIndex); // invoke when switch Fragment by hand + mFragmentContainerHelper.handlePageSelected(pageIndex); // invoke when switch Fragment ``` # Extend diff --git a/app/src/main/java/net/lucode/hackware/magicindicatordemo/MainActivity.java b/app/src/main/java/net/lucode/hackware/magicindicatordemo/MainActivity.java index a0f0e01e..ba18aa44 100644 --- a/app/src/main/java/net/lucode/hackware/magicindicatordemo/MainActivity.java +++ b/app/src/main/java/net/lucode/hackware/magicindicatordemo/MainActivity.java @@ -18,7 +18,7 @@ import net.lucode.hackware.magicindicator.FragmentContainerHelper; import net.lucode.hackware.magicindicator.MagicIndicator; -import net.lucode.hackware.magicindicator.SimpleViewPagerDelegate; +import net.lucode.hackware.magicindicator.ViewPagerHelper; import net.lucode.hackware.magicindicator.buildins.UIUtil; import net.lucode.hackware.magicindicator.buildins.circlenavigator.CircleNavigator; import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator; @@ -137,7 +137,7 @@ public IPagerIndicator getIndicator(Context context) { } }); magicIndicator.setNavigator(commonNavigator); - SimpleViewPagerDelegate.with(magicIndicator, mViewPager).delegate(); + ViewPagerHelper.bind(magicIndicator, mViewPager); // 当前页不定位到中间 final MagicIndicator magicIndicator1 = (MagicIndicator) findViewById(R.id.magic_indicator1); @@ -175,7 +175,7 @@ public IPagerIndicator getIndicator(Context context) { } }); magicIndicator1.setNavigator(commonNavigator1); - SimpleViewPagerDelegate.with(magicIndicator1, mViewPager).delegate(); + ViewPagerHelper.bind(magicIndicator1, mViewPager); // 当前页始终定位到中间 final MagicIndicator magicIndicator2 = (MagicIndicator) findViewById(R.id.magic_indicator2); @@ -222,7 +222,7 @@ public IPagerIndicator getIndicator(Context context) { } }); magicIndicator2.setNavigator(commonNavigator2); - SimpleViewPagerDelegate.with(magicIndicator2, mViewPager).delegate(); + ViewPagerHelper.bind(magicIndicator2, mViewPager); // 动态增加、删除小红点 commonNavigator2.postDelayed(new Runnable() { @@ -315,7 +315,7 @@ public float getTitleWeight(Context context, int index) { titleContainer.setDividerPadding(UIUtil.dip2px(this, 15)); titleContainer.setDividerDrawable(getResources().getDrawable(R.drawable.simple_splitter)); -// SimpleViewPagerDelegate.with(magicIndicator3, mViewPager).delegate(); +// ViewPagerHelper.bind(magicIndicator3, mViewPager); mFragmentContainerHelper = new FragmentContainerHelper(magicIndicator3); mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @@ -369,7 +369,7 @@ public IPagerIndicator getIndicator(Context context) { } }); magicIndicator4.setNavigator(commonNavigator4); - SimpleViewPagerDelegate.with(magicIndicator4, mViewPager).delegate(); + ViewPagerHelper.bind(magicIndicator4, mViewPager); // 缩放 + 颜色渐变 final MagicIndicator magicIndicator5 = (MagicIndicator) findViewById(R.id.magic_indicator5); @@ -410,7 +410,7 @@ public IPagerIndicator getIndicator(Context context) { } }); magicIndicator5.setNavigator(commonNavigator5); - SimpleViewPagerDelegate.with(magicIndicator5, mViewPager).delegate(); + ViewPagerHelper.bind(magicIndicator5, mViewPager); // 只有指示器,没有title final MagicIndicator magicIndicator6 = (MagicIndicator) findViewById(R.id.magic_indicator6); @@ -436,7 +436,7 @@ public IPagerIndicator getIndicator(Context context) { } }); magicIndicator6.setNavigator(commonNavigator6); - SimpleViewPagerDelegate.with(magicIndicator6, mViewPager).delegate(); + ViewPagerHelper.bind(magicIndicator6, mViewPager); // 带吸附效果 final MagicIndicator magicIndicator7 = (MagicIndicator) findViewById(R.id.magic_indicator7); @@ -477,7 +477,7 @@ public IPagerIndicator getIndicator(Context context) { } }); magicIndicator7.setNavigator(commonNavigator7); - SimpleViewPagerDelegate.with(magicIndicator7, mViewPager).delegate(); + ViewPagerHelper.bind(magicIndicator7, mViewPager); // 贝塞尔曲线 final MagicIndicator magicIndicator8 = (MagicIndicator) findViewById(R.id.magic_indicator8); @@ -513,7 +513,7 @@ public IPagerIndicator getIndicator(Context context) { } }); magicIndicator8.setNavigator(commonNavigator8); - SimpleViewPagerDelegate.with(magicIndicator8, mViewPager).delegate(); + ViewPagerHelper.bind(magicIndicator8, mViewPager); // 天天快报式 final MagicIndicator magicIndicator9 = (MagicIndicator) findViewById(R.id.magic_indicator9); @@ -548,7 +548,7 @@ public IPagerIndicator getIndicator(Context context) { } }); magicIndicator9.setNavigator(commonNavigator9); - SimpleViewPagerDelegate.with(magicIndicator9, mViewPager).delegate(); + ViewPagerHelper.bind(magicIndicator9, mViewPager); // 小尖角式 final MagicIndicator magicIndicator10 = (MagicIndicator) findViewById(R.id.magic_indicator10); @@ -583,7 +583,7 @@ public IPagerIndicator getIndicator(Context context) { } }); magicIndicator10.setNavigator(commonNavigator10); - SimpleViewPagerDelegate.with(magicIndicator10, mViewPager).delegate(); + ViewPagerHelper.bind(magicIndicator10, mViewPager); // 圆圈式 final MagicIndicator magicIndicator11 = (MagicIndicator) findViewById(R.id.magic_indicator11); @@ -591,13 +591,13 @@ public IPagerIndicator getIndicator(Context context) { circleNavigator.setCircleCount(mDataList.size()); circleNavigator.setCircleColor(Color.RED); magicIndicator11.setNavigator(circleNavigator); - SimpleViewPagerDelegate.with(magicIndicator11, mViewPager).delegate(); + ViewPagerHelper.bind(magicIndicator11, mViewPager); MagicIndicator magicIndicator13 = (MagicIndicator) findViewById(R.id.magic_indicator13); ScaleCircleNavigator scaleCircleNavigator = new ScaleCircleNavigator(this); scaleCircleNavigator.setCircleCount(mDataList.size()); magicIndicator13.setNavigator(scaleCircleNavigator); - SimpleViewPagerDelegate.with(magicIndicator13, mViewPager).delegate(); + ViewPagerHelper.bind(magicIndicator13, mViewPager); // 通用式 final MagicIndicator magicIndicator12 = (MagicIndicator) findViewById(R.id.magic_indicator12); @@ -661,6 +661,6 @@ public IPagerIndicator getIndicator(Context context) { } }); magicIndicator12.setNavigator(commonNavigator12); - SimpleViewPagerDelegate.with(magicIndicator12, mViewPager).delegate(); + ViewPagerHelper.bind(magicIndicator12, mViewPager); } } diff --git a/magicindicator/src/main/java/net/lucode/hackware/magicindicator/SimpleViewPagerDelegate.java b/magicindicator/src/main/java/net/lucode/hackware/magicindicator/SimpleViewPagerDelegate.java deleted file mode 100644 index 9f055bac..00000000 --- a/magicindicator/src/main/java/net/lucode/hackware/magicindicator/SimpleViewPagerDelegate.java +++ /dev/null @@ -1,42 +0,0 @@ -package net.lucode.hackware.magicindicator; - -import android.support.v4.view.ViewPager; - -/** - * ViewPager委托类,使得MagicIndicator和ViewPager集成更方便 - * Created by hackware on 2016/8/17. - */ - -public class SimpleViewPagerDelegate { - private ViewPager mViewPager; - private MagicIndicator mMagicIndicator; - - private SimpleViewPagerDelegate(MagicIndicator magicIndicator, ViewPager viewPager) { - mMagicIndicator = magicIndicator; - mViewPager = viewPager; - } - - public static SimpleViewPagerDelegate with(MagicIndicator magicIndicator, ViewPager viewPager) { - return new SimpleViewPagerDelegate(magicIndicator, viewPager); - } - - public void delegate() { - mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { - - @Override - public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { - mMagicIndicator.onPageScrolled(position, positionOffset, positionOffsetPixels); - } - - @Override - public void onPageSelected(int position) { - mMagicIndicator.onPageSelected(position); - } - - @Override - public void onPageScrollStateChanged(int state) { - mMagicIndicator.onPageScrollStateChanged(state); - } - }); - } -} diff --git a/magicindicator/src/main/java/net/lucode/hackware/magicindicator/ViewPagerHelper.java b/magicindicator/src/main/java/net/lucode/hackware/magicindicator/ViewPagerHelper.java new file mode 100644 index 00000000..24c975b2 --- /dev/null +++ b/magicindicator/src/main/java/net/lucode/hackware/magicindicator/ViewPagerHelper.java @@ -0,0 +1,30 @@ +package net.lucode.hackware.magicindicator; + +import android.support.v4.view.ViewPager; + +/** + * 简化和ViewPager绑定 + * Created by hackware on 2016/8/17. + */ + +public class ViewPagerHelper { + public static void bind(final MagicIndicator magicIndicator, ViewPager viewPager) { + viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { + + @Override + public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { + magicIndicator.onPageScrolled(position, positionOffset, positionOffsetPixels); + } + + @Override + public void onPageSelected(int position) { + magicIndicator.onPageSelected(position); + } + + @Override + public void onPageScrollStateChanged(int state) { + magicIndicator.onPageScrollStateChanged(state); + } + }); + } +}