Vlad
Vlad

Reputation: 910

ViewPager and PageTitleStrip

So,I am trying to create Swipe tabs using ViewPager from suppoer.v4 library.

Everything works as meant,BUT the title-bar and tabs are not showing. I know that the tabs bar is there because like 2-3 pixels are shown of it (I set a specific color,that's how I know), but it's not fully shown, nor is the title.

I will link my MainActivity.java ,activity_main.xml and if anyone asks,I will add the two fragments.But I doubt that is the problem as they have nothing in there besides a linear layout and a textView.

MainActivity.java:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

public class MainActivity extends FragmentActivity {

    ViewPager viewPager=null;

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


        viewPager = (ViewPager) findViewById(R.id.viewPager);
        FragmentManager fm = getSupportFragmentManager();
        viewPager.setAdapter(new Adaptor(fm));
    }

    class Adaptor extends FragmentPagerAdapter{


        Fragment fragment = null;

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

        @Override
        public Fragment getItem(int i) {

            if(i==0){
                fragment = new OpiniaTa();
            }
            if(i==1){
                fragment = new Test();
            }

            return fragment;
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {

            if(position == 0){
                return "Opinia ta";
            }
            if(position == 1){
                return "test";
            }

            return null;
        }
    }

}

activity_main.xml:

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

    <android.support.v4.view.PagerTitleStrip
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/titleTabs"
        android:background="#33B5E5"
        android:foregroundGravity="top"
        android:paddingTop="4dp"
        android:paddingBottom="4dp"
        >
    </android.support.v4.view.PagerTitleStrip>

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

If any other information needed, let me know, I will be as prompt as possible.

Upvotes: 0

Views: 1040

Answers (2)

arbrcr
arbrcr

Reputation: 815

Could you post a screenshot of the result. I am wondering if its just the content in tabs or the tabs itself are not displayed.

There is an issue on 23.0.0 with text in tab missing https://code.google.com/p/android/issues/detail?id=183127 https://code.google.com/p/android/issues/detail?id=184431

Maybe your issue is not related to this. Just want to make sure.

Alternatively, use the below link as suggested the poster. Probably wrap_content to height...

How can we work-around the blank title in PagerTitleStrip and PagerTabStrip?

Upvotes: 1

Neo
Neo

Reputation: 1469

I think in Adaptor class, you should make a constructor include list of fragments. The following code is what I am doing this. Hope this help.

public class PagerAdapter extends FragmentPagerAdapter {

    private  List<Fragment> fragments;

    public PagerAdapter(FragmentManager fm, List<Fragment> fragmentList) {
        super(fm);

        this.fragments = fragmentList;
    }

    @Override
    public Fragment getItem(int i) {
        return this.fragments.get(i);
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "Opinia ta";
            case 1:
                return "test";
            default:
                return null;
        }
    }
}

And in MainActivity class:

 private  PagerAdapter mPagerAdapter;

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

    private void initializePaging() {
        List<Fragment> fragments = new Vector<Fragment>();
        fragments.add(Fragment.instantiate(this, WebServiceFragment.class.getName()));
        fragments.add(Fragment.instantiate(this, CMCSMOFragment.class.getName()));

        mPagerAdapter = new PagerAdapter(this.getSupportFragmentManager(), fragments);

        ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
        pager.setAdapter(mPagerAdapter);
    }

Upvotes: 0

Related Questions