Skip to content

Commit

Permalink
Save attached adapter in ViewPagerAttacher to detach it later correct…
Browse files Browse the repository at this point in the history
…ly. Gradle update.
  • Loading branch information
Nikita Olifer committed Jun 22, 2018
1 parent 1a00b90 commit 19a0e19
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 21 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.android.tools.build:gradle:3.1.3'
}
}

Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Jan 17 13:31:47 MSK 2018
#Fri Jun 22 12:47:41 MSK 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class RecyclerViewAttacher implements ScrollingPagerIndicator.PagerAttach
private ScrollingPagerIndicator indicator;
private RecyclerView recyclerView;
private LinearLayoutManager layoutManager;
private RecyclerView.Adapter<?> adapter;
private RecyclerView.Adapter<?> attachedAdapter;

private RecyclerView.OnScrollListener scrollListener;
private RecyclerView.AdapterDataObserver dataObserver;
Expand Down Expand Up @@ -72,13 +72,13 @@ public void attachToPager(@NonNull final ScrollingPagerIndicator indicator, @Non
throw new IllegalStateException("Only HORIZONTAL orientation is supported");
}
this.recyclerView = pager;
this.adapter = pager.getAdapter();
this.attachedAdapter = pager.getAdapter();
this.indicator = indicator;

dataObserver = new RecyclerView.AdapterDataObserver() {
@Override
public void onChanged() {
indicator.setDotCount(adapter.getItemCount());
indicator.setDotCount(attachedAdapter.getItemCount());
updateCurrentOffset();
}

Expand Down Expand Up @@ -107,8 +107,8 @@ public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
onChanged();
}
};
adapter.registerAdapterDataObserver(dataObserver);
indicator.setDotCount(adapter.getItemCount());
attachedAdapter.registerAdapterDataObserver(dataObserver);
indicator.setDotCount(attachedAdapter.getItemCount());
updateCurrentOffset();

scrollListener = new RecyclerView.OnScrollListener() {
Expand All @@ -117,8 +117,8 @@ public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
if (newState == RecyclerView.SCROLL_STATE_IDLE && isInIdleState()) {
int newPosition = findCompletelyVisiblePosition();
if (newPosition != RecyclerView.NO_POSITION) {
indicator.setDotCount(adapter.getItemCount());
if (newPosition < adapter.getItemCount()) {
indicator.setDotCount(attachedAdapter.getItemCount());
if (newPosition < attachedAdapter.getItemCount()) {
indicator.setCurrentPosition(newPosition);
}
}
Expand All @@ -136,7 +136,7 @@ public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

@Override
public void detachFromPager() {
adapter.unregisterAdapterDataObserver(dataObserver);
attachedAdapter.unregisterAdapterDataObserver(dataObserver);
recyclerView.removeOnScrollListener(scrollListener);
measuredChildWidth = 0;
}
Expand All @@ -151,7 +151,7 @@ private void updateCurrentOffset() {
if (position == RecyclerView.NO_POSITION) {
return;
}
final int itemCount = adapter.getItemCount();
final int itemCount = attachedAdapter.getItemCount();

// In case there is an infinite pager
if (position >= itemCount && itemCount != 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ public class ViewPagerAttacher implements ScrollingPagerIndicator.PagerAttacher<
private DataSetObserver dataSetObserver;
private ViewPager.OnPageChangeListener onPageChangeListener;
private ViewPager pager;
private PagerAdapter attachedAdapter;

@Override
public void attachToPager(@NonNull final ScrollingPagerIndicator indicator, @NonNull final ViewPager pager) {
final PagerAdapter adapter = pager.getAdapter();
if (adapter == null) {
attachedAdapter = pager.getAdapter();
if (attachedAdapter == null) {
throw new IllegalStateException("Set adapter before call attachToPager() method");
}

this.pager = pager;

indicator.setDotCount(adapter.getCount());
indicator.setDotCount(attachedAdapter.getCount());
indicator.setCurrentPosition(pager.getCurrentItem());

dataSetObserver = new DataSetObserver() {
Expand All @@ -37,7 +38,7 @@ public void onInvalidated() {
onChanged();
}
};
adapter.registerDataSetObserver(dataSetObserver);
attachedAdapter.registerDataSetObserver(dataSetObserver);

onPageChangeListener = new ViewPager.OnPageChangeListener() {

Expand All @@ -60,7 +61,7 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse
@Override
public void onPageSelected(int position) {
if (idleState) {
indicator.setDotCount(adapter.getCount());
indicator.setDotCount(attachedAdapter.getCount());
indicator.setCurrentPosition(pager.getCurrentItem());
}
}
Expand All @@ -75,10 +76,7 @@ public void onPageScrollStateChanged(int state) {

@Override
public void detachFromPager() {
PagerAdapter adapter = pager.getAdapter();
if (adapter != null) {
adapter.unregisterDataSetObserver(dataSetObserver);
}
attachedAdapter.unregisterDataSetObserver(dataSetObserver);
pager.removeOnPageChangeListener(onPageChangeListener);
}
}

0 comments on commit 19a0e19

Please sign in to comment.