【发布时间】:2015-07-30 17:22:06
【问题描述】:
我按照这些guidelines 创建了一个带有片段的滑动选项卡布局。问题是我想向 SlidingTabLayout 动态添加一个选项卡。以下代码创建了一个片段,但不显示。如何将它附加到 SlidingTabLayout?
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
tableFragment = (ReportTableFragment) adapter.getItem(1);
fragmentTransaction.replace(R.id.pager, tableFragment, tableFragment.getClass().getName());
fragmentTransaction.commit();
这是我的 ViewPagerAdapter 类
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created
int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created
// Build a Constructor and assign the passed Values to appropriate values in the class
public ViewPagerAdapter(FragmentManager fm,CharSequence mTitles[], int mNumbOfTabsumb) {
super(fm);
this.Titles = mTitles;
this.NumbOfTabs = mNumbOfTabsumb;
}
//This method return the fragment for the every position in the View Pager
@Override
public Fragment getItem(int position) {
if(position == 0) // if the position is 0 we are returning the First tab
{
//ReportMapFragment reportMapTab = new ReportMapFragment();
//return reportMapTab;
SupportMapFragment mapTab = new SupportMapFragment();
return mapTab;
}
else
{
return ReportTableFragment.newInstance(1);
}
}
// This method return the titles for the Tabs in the Tab Strip
@Override
public CharSequence getPageTitle(int position) {
return Titles[position];
}
// This method return the Number of tabs for the tabs Strip
@Override
public int getCount() {
return NumbOfTabs;
}
}
【问题讨论】:
标签: android android-fragments tabs