0
我實現標籤:延遲片片斷創建
public class SectionsPagerAdapter extends FragmentPagerAdapter {
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0: {
fragment = new Fragment1();
break;
}
case 1: {
fragment = new Fragment2();
break;
}
}
return fragment;
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return "Fragment1";
case 1:
return "Fragment2";
}
return null;
}
}
活動:
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
Tab tab = actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(i));
tab.setTabListener(this);
actionBar.addTab(tab, i == 0); // Select first tab
}
什麼我發現是,每個視圖中創建了前面,即在創建視圖之前它的標籤是選擇。有沒有辦法延遲創建未被選中的標籤頁?
感謝您的答覆!對不起,我現在意識到我的問題可能一直令人困惑。我一直在尋找一種方法來延遲片段創建,直到單擊此選項卡,而不是基於時間。 IE瀏覽器,如果我有5個選項卡,我可以節省初始加載時間,因爲我只是最初創建第一個選項卡,因爲這是唯一顯示的選項卡。 – lostintranslation