问题
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