PagerTabStrip adds two strips in Fullscreen instead of one strip

349 Views Asked by At

I'm trying to implement PagerTabStrip in my Android Studio project. I am using a Fullscreen Activity, but two Tab strips are added, the second one empty, as shown in this photo:enter image description here

If I use a regular activity, only one Tab Strip is added, which is what I want: enter image description here

The second photo is from a sample Google project, but I created a new fullscreen activity project and a new regular activity project and got the same results. I used the same code from the Google Project throughout.

From my fullscreen activity above, setting up the ViewPager and PagerTabStrip in the XML:

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

    <android.support.v4.view.PagerTabStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_gravity="top"
        android:textColor="#fff"
        android:paddingTop="4dp"
        android:paddingBottom="4dp" />

</android.support.v4.view.ViewPager>

And in the FullscreenActivity.java:

MyAdapter mAdapter;
ViewPager mPager;

...onCreate...{
    ...
    mAdapter = new MyAdapter(getSupportFragmentManager());
    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);
}

And then the two classes I need for this:

public static class MyAdapter extends FragmentStatePagerAdapter {
    public MyAdapter(FragmentManager frag) {
        super(frag);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = new DemoObjectFragment();
        Bundle args = new Bundle();
        args.putInt(DemoObjectFragment.ARG_OBJECT, position + 1);
        fragment.setArguments(args);
        return fragment;
    }

    public int getCount() {
        return 5;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0: {page = "Page 1";
                break;}
            case 1: {page = "Page 2";
                break;}
            case 2: {page = "Page 3";
                break;}
            case 3: {page = "Page 4";
                break;}
            case 4: {page = "Page 5";
                break;}
        }
        return page;
    }
}

public static class DemoObjectFragment extends Fragment {
    public static final String ARG_OBJECT = "object";

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_fullscreen, container, false);
    return rootView;
    }
}

What can I do to get rid of this second bar? I am pretty much at a dead end, having tried stripping out xml elements and java code. Thanks

1

There are 1 best solutions below

0
On

I found out why two Tab Strips are showing up. When I call the DemoObjectFragment class, under the new View rootView I create I am inflating the activity_fullscreen layout, which is the main layout! That means I'm inflating the screen with the tabstrip over the screen with the tabstrip. If I create a new layout file, i.e. layout_fragment1 and set this under View rootView = inflater.inflate(R.layout.layout_fragment1, container, false);, then this will be the layout that is inflated and since it does not have a tab strip, that strip will not be created again. I'm still not sure why the non-fullscreen app is behaving differently, but this is a step forward!