我有在它兩個片段和一個滑動片佈局的FragmentActivity:Android在加載後添加片段和標籤?
String titles[] = new String[] {"Tab One", "Tab Two"};
int numTabs = titles.length;
EventAdapter adapter = new EventAdapter(getSupportFragmentManager(), titles, numTabs);
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
SlidingTabLayout sliding_tabs = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
sliding_tabs.setDistributeEvenly(true);
sliding_tabs.setViewPager(pager);
隨着FragmentPagerAdapter:
private class EventAdapter extends FragmentPagerAdapter {
private List<String> titles;
private int numTabs;
public void addTab (String title) {
this.titles.add(title);
this.numTabs++;
this.notifyDataSetChanged();
}
public EventAdapter(FragmentManager fm, List<String> mTitles, int mNumTabs) {
super(fm);
this.titles = mTitles;
this.numTabs = mNumTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 2:
return new FragThree();
case 1:
return new FragTwo();
default:
return new FragOne();
}
}
@Override
public String getPageTitle(int position) {
return titles.get(position);
}
@Override
public int getCount() {
return numTabs;
}
}
有的時候下了線,我想動態添加第三個選項卡到這個佈局。有沒有辦法在代碼中做到這一點?如您所見,我已經設置了PagerAdapter來捕捉第三個選項卡。我只是需要將其加載到...
[This may help you。](http://android-er.blogspot.in/2014/03/viewpager-example-with-switching-page.html) –
@MD我沒有看到任何關於在尋呼機上添加製表符... –
您是否看到過'TabsAdapter'中的addTab(...)'函數? –