4

I was wanting to use ActionBar tabs and ViewPager to implement swiping between tabs. I have successfully used the example here:

http://developer.android.com/reference/android/support/v4/view/ViewPager.html

However, I also wanted to add a Frame Layout outside of the ViewPager in the xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/FrameLayout1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </android.support.v4.view.ViewPager>   

    <FrameLayout
        android:id="@+id/frame_layout"
        android:name="com.banagas.calculator.ButtonFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>

If I set the setContentView() to my layout with both the ViewPager and Frame Layout, the ViewPager stops functioning:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i("MainActivity", "onCreate");  
        mViewPager = new ViewPager(this);
        mViewPager.setId(R.id.FrameLayout1);
        ActionBar bar = getSupportActionBar();
        mViewPager.setOffscreenPageLimit(4);

        bar.setDisplayHomeAsUpEnabled(false);
        bar.setDisplayShowTitleEnabled(false);
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        mTabsAdapter = new TabsAdapter(this, mViewPager);

        mTabsAdapter.addTab(bar.newTab().setText("Display"), DisplayFragment.class, null);
        mTabsAdapter.addTab(bar.newTab().setText("Y="), YFragment.class, null);
        mTabsAdapter.addTab(bar.newTab().setText("Graph"), GraphFragment.class, null);
        mTabsAdapter.addTab(bar.newTab().setText("Table"), TableFragment.class, null);

        if (findViewById(R.id.frame_layout) != null) 
        {
            if (savedInstanceState != null) 
            {
                return;
            }
            getSupportFragmentManager().beginTransaction().add(R.id.frame_layout, new ButtonFragment()).commit();
            Log.i("MainActivity", "add.ButtonFragment");
        }
    }

I can see the tabs and nothing else within the ViewPager. My Frame Layout works perfectly fine though. How would I get ViewPager to work with this?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
vabanagas
  • 811
  • 7
  • 6
  • probably not related to bug, but: 1. Why you added the fragment name(ButtonFragment) for FrameLayout? 2. It is better to check savedInstanceState != null first and only then search for the view. – vovkab Dec 24 '12 at 20:44
  • I am actually using the FrameLayout to switch between two fragments on a button click and I formatted it that way because I was told that it was the correct way of doing it I will try the other way though. – vabanagas Dec 24 '12 at 20:57
  • What does "nothing else within the ViewPager" mean? Is the FragmentLayout taking all space or are the fragments not created? – Wolfram Rittmeyer Dec 24 '12 at 23:26
  • The Fragments simply are not created. The tabs are created and there is no problem with the layout. The ViewPager just won't implement the Fragments. – vabanagas Dec 25 '12 at 09:14

1 Answers1

1

Why you do mViewPager = new ViewPager(this); mViewPager.setId(R.id.FrameLayout1); ?

The ViewPager is already on your layout.

Shoud do something like mViewPager = (ViewPager)findViewById(R.id.FrameLayout1);

From the sample code they are creating the controls on runtime, while you have them in the xml.

soynerdito
  • 501
  • 3
  • 10