maysi
maysi

Reputation: 5517

How to add a tab to SlidingTabLayout?

I am using google's SlidingTabLayout in my view, but i want to add dynamically new tabs to it. I'm using this http://developer.android.com/samples/SlidingTabsBasic/src/com.example.android.common/view/SlidingTabLayout.html

This is my code which initialize it:

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 
    //this ^ is a FragmentStatePagerAdapter

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    // Assiging the Sliding Tab Layout View
    tabs = (SlidingTabLayout) findViewById(R.id.tabs);

    // Setting the ViewPager For the SlidingTabsLayout
    tabs.setViewPager(mViewPager);

I have absolutely no idea how to add a Tab.

I thought about changing the ViewPager but in the SlidingTabLayout there is a comment:

/**
 * Sets the associated view pager. Note that the assumption here is that the pager content
 * (number of tabs and tab titles) does not change after this call has been made.
 */
public void setViewPager(ViewPager viewPager) {

Upvotes: 1

Views: 1769

Answers (2)

Colin White
Colin White

Reputation: 1116

There's probably a nicer, more efficient way, but simply calling setViewPager() every time you add a page to your ViewPager will work.

Upvotes: 3

t3rse
t3rse

Reputation: 10124

Here is a nice tutorial. http://www.android4devs.com/2015/01/how-to-make-material-design-sliding-tabs.html

In short, you can supply them with your Pager Adapter. Override the getPageTitle method in order to supply something custom. E.g.

@Override
public CharSequence getPageTitle(int position) {
    return _items[position].getDisplayName();
}

Upvotes: 0

Related Questions