Permalink
Browse files

fix removing views from ViewPagerAndroid

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...
1 parent 03a2f61 commit b0c023c85c9a1c675b6787c6292400c5fd229181 @foghina foghina committed with Facebook Github Bot 0 Jul 14, 2016
@@ -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);
@@ -68,6 +68,11 @@ public int getCount() {
}
@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);
container.addView(view, 0, generateDefaultLayoutParams());
@@ -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

0 comments on commit b0c023c

Please sign in to comment.