Skip to content

Commit

Permalink
Fix ViewPager behavior with Nodes
Browse files Browse the repository at this point in the history
Summary: Add a batch addition operation for ViewPager.

Differential Revision: D3597840

fbshipit-source-id: 1c9c42e03da2492444298220e75f547b6567b4e5
  • Loading branch information
Ahmed El-Helw authored and Facebook Github Bot 7 committed Jul 22, 2016
1 parent 32a7178 commit 5f41769
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@

package com.facebook.react.uimanager;

import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.WeakHashMap;

import android.view.View;
import android.view.ViewGroup;

/**
* Class providing children management API for view managers of classes extending ViewGroup.
*/
Expand Down Expand Up @@ -45,6 +46,19 @@ public void addView(T parent, View child, int index) {
reorderChildrenByZIndex(parent);
}

/**
* Convenience method for batching a set of addView calls
* Note that this adds the views to the beginning of the ViewGroup
*
* @param parent the parent ViewGroup
* @param views the set of views to add
*/
public void addViews(T parent, List<View> views) {
for (int i = 0, size = views.size(); i < size; i++) {
addView(parent, views.get(i), i);
}
}

public static void setViewZIndex(View view, int zIndex) {
mZIndexHash.put(view, zIndex);
// zIndex prop gets set BEFORE the view is added, so parent may be null.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@
* views to custom {@link PagerAdapter} instance which is used by {@link NativeViewHierarchyManager}
* to add children nodes according to react views hierarchy.
*/
/* package */ class ReactViewPager extends ViewPager {
public class ReactViewPager extends ViewPager {

private class Adapter extends PagerAdapter {

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

void addView(View child, int index) {
mViews.add(index, child);
Expand All @@ -57,6 +58,32 @@ void removeViewAt(int index) {
setOffscreenPageLimit(mViews.size());
}

/**
* Replace a set of views to the ViewPager adapter and update the ViewPager
*/
void setViews(List<View> views) {
mViews.clear();
mViews.addAll(views);
notifyDataSetChanged();

// we want to make sure we return POSITION_NONE for every view here, since this is only
// called after a removeAllViewsFromAdapter
mIsViewPagerInIntentionallyInconsistentState = false;
}

/**
* Remove all the views from the adapter and de-parents them from the ViewPager
* After calling this, it is expected that notifyDataSetChanged should be called soon
* afterwards.
*/
void removeAllViewsFromAdapter(ViewPager pager) {
mViews.clear();
pager.removeAllViews();
// set this, so that when the next addViews is called, we return POSITION_NONE for every
// entry so we can remove whichever views we need to and add the ones that we need to.
mIsViewPagerInIntentionallyInconsistentState = true;
}

View getViewAt(int index) {
return mViews.get(index);
}
Expand All @@ -68,7 +95,9 @@ public int getCount() {

@Override
public int getItemPosition(Object object) {
return mViews.contains(object) ? mViews.indexOf(object) : POSITION_NONE;
// if we've removed all views, we want to return POSITION_NONE intentionally
return mIsViewPagerInIntentionallyInconsistentState || !mViews.contains(object) ?
POSITION_NONE : mViews.indexOf(object);
}

@Override
Expand Down Expand Up @@ -190,4 +219,12 @@ public void setScrollEnabled(boolean scrollEnabled) {
/*package*/ View getViewFromAdapter(int index) {
return getAdapter().getViewAt(index);
}

public void setViews(List<View> views) {
getAdapter().setViews(views);
}

public void removeAllViewsFromAdapter() {
getAdapter().removeAllViewsFromAdapter(this);
}
}

0 comments on commit 5f41769

Please sign in to comment.