How to add a tab to SlidingTabLayout?

落花浮王杯 提交于 2020-01-03 02:29:06

问题


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) {

回答1:


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




回答2:


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();
}


来源:https://stackoverflow.com/questions/28439621/how-to-add-a-tab-to-slidingtablayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!