1

I am trying to dynamically add View Pagers to my app by inflating XML. All the view pagers and corresponding adapters get instantiated correctly and the required fragments also get attached. However, only the fragments for the 1st view pager are visible. For the other view pagers, I can even swipe from one fragment to the next. They're just not visible.

public class MainActivity extends FragmentActivity {
    private static Integer NUM_PAGERS = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //get layout inflater
        LayoutInflater inflater = LayoutInflater.from(this);
        LinearLayout fragmentPagerLayout = (LinearLayout)     findViewById(R.id.linearLayoutMain);

        for(int index = 0; index < NUM_PAGERS; index++) {
            LinearLayout pagerLayout = (LinearLayout) inflater.inflate(R.layout.view_pager, null);

            //header textview
            TextView pagerHeaderTextView = (TextView) pagerLayout.findViewById(R.id.headerTextView);
            pagerHeaderTextView.setText("Header #" + index);

            //set the pager's adapter
            UninterceptableViewPager viewPager = (UninterceptableViewPager) pagerLayout.findViewById(R.id.pager);
            TestFragmentPagerAdapter pagerAdapter = new TestFragmentPagerAdapter(this.getSupportFragmentManager(), index);
            viewPager.setAdapter(pagerAdapter);

            fragmentPagerLayout.addView(pagerLayout);
        }
    }
} 

TestFragment's onCreateView and onAttach methods

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_layout, container, false);

    TextView titleText = (TextView) view.findViewById(R.id.titleText);
    titleText.setText(getArguments().getString("title"));

    return view;
}

public void onAttach(Activity activity) {
    super.onAttach(activity);

    Log.i("ViewPagerTest", "attached");
}

and the TestFragmentPagerAdapter's getItem method

public Fragment getItem(int position) {
    String title = titles[position];
    Log.i("ViewPagerTest", "Adapter #" + id + "--" + title);
    return TestFragment.newInstance(title, position);
}

This is the view pager layout

view_pager.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/headerTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <com.example.viewpagertest.UninterceptableViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="100dip"
        android:paddingBottom="5dip"
        android:background="@drawable/custom_shape" />

</LinearLayout>

And finally, here's the log. It shows that when I swipe across the 2nd view pager, a new fragment is attached just like for the 1st view pager. I even checked the variable state for all pagers and adapters, and I don't see how the 1st pager is different from the others.

I/ViewPagerTest(8304): Adapter #0--title 1
I/ViewPagerTest(8304): Adapter #0--title 2
I/ViewPagerTest(8304): attached
I/ViewPagerTest(8304): attached
I/ViewPagerTest(8304): Adapter #1--title 1
I/ViewPagerTest(8304): Adapter #1--title 2
I/ViewPagerTest(8304): attached
I/ViewPagerTest(8304): attached
I/ViewPagerTest(8304): Adapter #0--title 3
I/ViewPagerTest(8304): attached
I/ViewPagerTest(8304): Adapter #1--title 3
I/ViewPagerTest(8304): attached

The log above is only for 2 view pagers, but the results are the same for any number of adapters.

If I add all the view pagers to the XML directly instead of inflating, they all display without any problem.

EDIT

Since I don't have enough reputation to post a screenshot, here's what it looks like. As I mentioned earlier, the fragments in the 1st view pager are visible. However, as you can see below, they're not visible in the 2nd view pager even though they've been attached correctly and I can swipe through them.

Header #0
----------------------------
| title 1                  |
----------------------------

Header #1
----------------------------
|                          |
----------------------------
Sandeep
  • 11
  • 3

3 Answers3

2

You must use nested fragments, to embed a fragment inside a parent fragment. Call in the parent fragment getChildFragmentManager() instead getFragmentManager() to get the working FragmentManager.

Pelanes
  • 3,451
  • 1
  • 34
  • 32
1

Try extending [FragmentPagerAdapter][1] in your adapter as it will manage your instances. Check specially this method when setting the adapter your_adapter.setOffscreenPageLimit(# fragments);

Neron T
  • 369
  • 2
  • 8
0

One simple solution that may help. Call setAdapter again after you change it.

viewPager.setAdapter(pagerAdapter);
T D Nguyen
  • 7,054
  • 4
  • 51
  • 71
  • I'm not sure exactly what you mean, but I tried setting the adapters after all view pagers have been added to the root layout. No changes though. – Sandeep May 31 '13 at 08:31