1

I am using react-native and for layout purposes, unfortunately, I am forced to bridge to some native android code.

The problem I am facing is that the ViewPager only displays the first 2 pages (fragments). If I swipe to the third, it's empty. If I go back to the first it's not there anymore and finally, getting to the fourth and nothing is visible anymore.
This is of course as a result of only the first 2 fragments actually being drawn that are actually drawn/visible).
Also, the fragment onCreateView method is called. Moreover, if I set setOffscreenPageLimit(*) to the number of pages I have, they are perfectly drawn.
I guess it has to do with the ViewPager no aware of pages change.

I've tried all the tricks I could find on the web (some examples):

  1. (getChildFragmentManager instead of getSupportFragmentManager) The method getChildFragmentManager() is undefined
  2. (POSITION_NONE) ViewPager PagerAdapter not updating the View

And many many more...

Nothing seems to be working. I would love some help.... Here is all the code involved:

I am using a simple fragmented ViewPager :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layoutDirection="ltr"
    android:orientation="vertical">

    <android.support.v4.view.ViewPager
        android:id="@+id/pager2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v4.view.ViewPager>
</LinearLayout>

The view manager is straight forward as well:

public class DayViewManager extends SimpleViewManager<MyView> {
    public static final String REACT_CLASS = "MyView";
    private ThemedReactContext mContext;
    private View mView;

    @Override
    public String getName() {
        return REACT_CLASS;
    }

    @Override
    public MyView createViewInstance(final ThemedReactContext context){
        return new MyView(context);
    }
}

And again the adapter is straight forward as well:

public class MyViewAdapter extends FragmentPagerAdapter {
    private List<Day> mDays;

    public MyViewAdapter(FragmentManager fm) {
        super(fm);
    }

    public void setData(List<Day> days) {
        this.mDays = days;
    }

    @Override
    public Fragment getItem(int position) {
        return DayFragment.newInstance(mDays.get(position));
    }

    @Override
    public int getCount() {
        return mDays != null ? mDays.size() : 0;
    }
}

Also, MyView is using a static SupportFragmentManager that is initialised by MainActivity:

public class MainActivity extends ReactFragmentActivity {
    private static FragmentManager sSupportFragmentManager;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sSupportFragmentManager = getSupportFragmentManager();
    } 

    public static FragmentManager getActivitySupportFragmentManager() {
        return sSupportFragmentManager;
    }
}

Last MyView Code:

public class MyView extends LinearLayout {
    private Context mContext;
    private List<Day> mDemoData = new ArrayList<>();
    private MyViewAdapter mAdapter;


    public MyView(Context context) {
        super(context);
        this.mContext = context;
        init();
    }

    public void init() {
        inflate(mContext, R.layout.day_view, this);
        mAdapter = new  MyViewAdapter(MainActivity.getActivitySupportFragmentManager());

        final ViewPager viewPager = (ViewPager) findViewById(R.id.pager2);
        mDemoData.add(new Day("Item1", "First item", 1.5));
        mDemoData.add(new Day("Item2", "Second item", 2.5));
        mDemoData.add(new Day("Item3", "Third item", 5.2));
        mDemoData.add(new Day("Item4", "Fourth item", 5.1));
        mDemoData.add(new Day("Item5", "Fifth item", 3.8));
        mAdapter.setData(mDemoData);
        viewPager.setAdapter(mAdapter);

        mAdapter.notifyDataSetChanged();

    }
}

** I don't think it actually relates to react-native however....

Tomer
  • 4,382
  • 5
  • 38
  • 48

0 Answers0