Search code examples
androidandroid-fragmentsandroid-viewpager

Why inside ViewPager lost pages in ViewPager inside ViewPager case?


I have a strange bug with disappears inside viewPager when I slide more than 2 page in outside viewpager. (limit in viewpager is 1) If I swipe only one page left or one page right is ok. The problem occurres when I slide outside the limit and the page is dropped and have to be recreated. How to fix it? I cannot increase limit too much. :(

I have:

//layout/outside.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">
        <android.support.v4.view.ViewPager
            android:id="@+id/outside_viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
</FrameLayout>

and:

//layout/inside.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <android.support.v4.view.ViewPager
        android:id="@+id/inside_viewpager"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />

        <TextView         
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

private static class InsidePagerAdapter extends FragmentPagerAdapter {

    private Context context;
    private final List<Element> mElements;

public InsidePagerAdapter(Context context, FragmentManager fragmentManager, List<Element> elements) {
    super(fragmentManager);
    this.context = context;
    mElements = elements;
}

@Override
public int getCount() {
    return mElements.size();
}

@Override
public InsideFragment getItem(int position) {
    return InsideFragment.newInstance(position);//InsideFragment uses layout/inside.xml
}

}


Solution

  • I fixed. For someone who will have same issues. when I create InsidePagerAdapter and OutsidePageAdapter I have to pass getChildFragmentManager() instead getFragmentManager()