diff --git a/CHANGELOG.md b/CHANGELOG.md index 4681b54..6ce79ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,17 @@ ChangeLog -------- +##### 1.2.0 +* Add properties : ```orientation```(default:horizontal) , ```gravity```(default:center) +* Remove auto register DataSetObserver. [#54](https://github.com/ongakuer/CircleIndicator/issues/54) + + Please change like this if you used dynamic adapter: +```java +viewpager.setAdapter(mAdapter); +indicator.setViewPager(viewpager); +mAdapter.registerDataSetObserver(indicator.getDataSetObserver()); +``` + ##### 1.1.9 (2016-04-16) * Fix bug ([#51](https://github.com/ongakuer/CircleIndicator/issues/51)) * Add Reset Adapter Sample diff --git a/circleindicator/src/main/java/me/relex/circleindicator/CircleIndicator.java b/circleindicator/src/main/java/me/relex/circleindicator/CircleIndicator.java index d0112b1..0b3c251 100644 --- a/circleindicator/src/main/java/me/relex/circleindicator/CircleIndicator.java +++ b/circleindicator/src/main/java/me/relex/circleindicator/CircleIndicator.java @@ -166,7 +166,6 @@ public void setViewPager(ViewPager viewPager) { createIndicators(); mViewpager.removeOnPageChangeListener(mInternalPageChangeListener); mViewpager.addOnPageChangeListener(mInternalPageChangeListener); - mViewpager.getAdapter().registerDataSetObserver(mInternalDataSetObserver); mInternalPageChangeListener.onPageSelected(mViewpager.getCurrentItem()); } } @@ -193,18 +192,19 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse mAnimatorOut.cancel(); } - if (mLastPosition >= 0) { - View currentIndicator = getChildAt(mLastPosition); + View currentIndicator; + if (mLastPosition >= 0 && (currentIndicator = getChildAt(mLastPosition)) != null) { currentIndicator.setBackgroundResource(mIndicatorUnselectedBackgroundResId); mAnimatorIn.setTarget(currentIndicator); mAnimatorIn.start(); } View selectedIndicator = getChildAt(position); - selectedIndicator.setBackgroundResource(mIndicatorBackgroundResId); - mAnimatorOut.setTarget(selectedIndicator); - mAnimatorOut.start(); - + if (selectedIndicator != null) { + selectedIndicator.setBackgroundResource(mIndicatorBackgroundResId); + mAnimatorOut.setTarget(selectedIndicator); + mAnimatorOut.start(); + } mLastPosition = position; } @@ -212,9 +212,16 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse } }; + public DataSetObserver getDataSetObserver() { + return mInternalDataSetObserver; + } + private DataSetObserver mInternalDataSetObserver = new DataSetObserver() { @Override public void onChanged() { super.onChanged(); + if (mViewpager == null) { + return; + } int newCount = mViewpager.getAdapter().getCount(); int currentCount = getChildCount(); diff --git a/sample/src/main/java/me/relex/circleindicator/sample/SamplePagerAdapter.java b/sample/src/main/java/me/relex/circleindicator/sample/SamplePagerAdapter.java index 5e22526..c00b64a 100755 --- a/sample/src/main/java/me/relex/circleindicator/sample/SamplePagerAdapter.java +++ b/sample/src/main/java/me/relex/circleindicator/sample/SamplePagerAdapter.java @@ -45,10 +45,6 @@ public SamplePagerAdapter(int count) { return textView; } - @Override public int getItemPosition(Object object) { - return POSITION_NONE; - } - public void addItem() { mSize++; notifyDataSetChanged(); diff --git a/sample/src/main/java/me/relex/circleindicator/sample/fragment/DynamicAdapterFragment.java b/sample/src/main/java/me/relex/circleindicator/sample/fragment/DynamicAdapterFragment.java index fb0d11a..49c1c01 100644 --- a/sample/src/main/java/me/relex/circleindicator/sample/fragment/DynamicAdapterFragment.java +++ b/sample/src/main/java/me/relex/circleindicator/sample/fragment/DynamicAdapterFragment.java @@ -8,8 +8,8 @@ import android.view.View; import android.view.ViewGroup; import me.relex.circleindicator.CircleIndicator; -import me.relex.circleindicator.sample.SamplePagerAdapter; import me.relex.circleindicator.sample.R; +import me.relex.circleindicator.sample.SamplePagerAdapter; public class DynamicAdapterFragment extends Fragment implements View.OnClickListener { @@ -26,12 +26,17 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, view.findViewById(R.id.add).setOnClickListener(this); view.findViewById(R.id.remove).setOnClickListener(this); - mAdapter = new SamplePagerAdapter(1); + mAdapter = new SamplePagerAdapter(1) { + @Override public int getItemPosition(Object object) { + return POSITION_NONE; + } + }; ViewPager viewpager = (ViewPager) view.findViewById(R.id.viewpager); CircleIndicator indicator = (CircleIndicator) view.findViewById(R.id.indicator); viewpager.setAdapter(mAdapter); indicator.setViewPager(viewpager); + mAdapter.registerDataSetObserver(indicator.getDataSetObserver()); } @Override public void onClick(View v) {