我已使用this示例作为指南。我已经修改了一些代码,所以现在我有了:
for loop
方法中修改后的onCreate
:
for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++)
{
bar.addTab(
bar.newTab()
.setText(tabsTitles[i])
.setTabListener(new android.support.v7.app.ActionBar.TabListener()
{
@Override
public void onTabSelected(android.support.v7.app.ActionBar.Tab tab, android.support.v4.app.FragmentTransaction fragmentTransaction)
{
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(android.support.v7.app.ActionBar.Tab tab, android.support.v4.app.FragmentTransaction fragmentTransaction)
{
}
@Override
public void onTabReselected(android.support.v7.app.ActionBar.Tab tab, android.support.v4.app.FragmentTransaction fragmentTransaction)
{
}
}));
}
List<android.support.v7.app.ActionBar.Tab> tabsToDelete = new
ArrayList<android.support.v7.app.ActionBar.Tab>();
for (int i = 0 ; i < tabsTitles.length ; i++)
{
if (tabsTitles[i].equals(res.getString(R.string.settings)) ||
tabsTitles[i].equals(res.getString(R.string.my_groups)))
{
tabsToDelete.add(bar.getTabAt(i));
}
}
for (android.support.v7.app.ActionBar.Tab tab : tabsToDelete)
{
bar.removeTab(tab);
}
}
标签会从栏中正确删除,但我仍然可以滑动到已删除的标签页。删除的标签内容暂时可见,然后应用程序崩溃:
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
{
@Override
public void onPageSelected(int position)
{
// When swiping between different app sections, select the corresponding tab.
// We can also use ActionBar.Tab#select() to do this if we have a reference to the
// Tab.
bar.setSelectedNavigationItem(position);
}
});