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
This commit is contained in:
Felix Oghina 2016-07-14 13:27:11 -07:00 committed by Facebook Github Bot 0
parent 03a2f6139f
commit b0c023c85c

View File

@ -33,7 +33,7 @@ import com.facebook.react.uimanager.events.NativeGestureUtil;
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);
@ -67,6 +67,11 @@ import com.facebook.react.uimanager.events.NativeGestureUtil;
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);
@ -76,8 +81,7 @@ import com.facebook.react.uimanager.events.NativeGestureUtil;
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
View view = mViews.get(position);
container.removeView(view);
container.removeView((View) object);
}
@Override