Skip to content

Commit

Permalink
fix removing views from ViewPagerAndroid
Browse files Browse the repository at this point in the history
Summary:
Two things in this diff:

1. Implemented `getItemPosition` in our adapter; the default implementation always returns POSITION_UNCHANGED, which is incorrect, and causes `destroyItem` to never (sometimes?) be called.
2. Fix `destroyItem`: this never worked. `destroyItem` is always called by the ViewPager after a `notifyDataSetChanged()`, so after `removeViewAt`, which removes the view from `mViews`, causing `destroyItem` to throw `IndexOutOfBoundsException` when it tries to get the view. Since our item objects are just views, use that instead of checking `mViews`.

Reviewed By: ahmedre

Differential Revision: D3555427

fbshipit-source-id: 900c2696162d07f507e850517d483b943ce39a35
  • Loading branch information
foghina authored and Facebook Github Bot 0 committed Jul 14, 2016
1 parent 03a2f61 commit b0c023c
Showing 1 changed file with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

private class Adapter extends PagerAdapter {

private List<View> mViews = new ArrayList<>();
private final List<View> mViews = new ArrayList<>();

void addView(View child, int index) {
mViews.add(index, child);
Expand Down Expand Up @@ -67,6 +67,11 @@ public int getCount() {
return mViews.size();
}

@Override
public int getItemPosition(Object object) {
return mViews.contains(object) ? mViews.indexOf(object) : POSITION_NONE;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = mViews.get(position);
Expand All @@ -76,8 +81,7 @@ public Object instantiateItem(ViewGroup container, int position) {

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
View view = mViews.get(position);
container.removeView(view);
container.removeView((View) object);
}

@Override
Expand Down

0 comments on commit b0c023c

Please sign in to comment.