我正在实施标签:
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
}
我发现每个视图都是预先创建的,即视图是在选中其选项卡之前创建的。有没有办法延迟创建尚未选择的标签?
答案 0 :(得分:0)
如果您只想延迟创建标签,请尝试以下方法:
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0: {
// 3000 = SECONDS DELAY
new CountDownTimer(3000, 1000) {
public void onTick(long millisUntilFinished) {}
public void onFinish() {
// INFLATE VIEW HERE
fragment = new Fragment1();
}
}.start();
break;
}
case 1: {
// 3000 = SECONDS DELAY
new CountDownTimer(3000, 1000) {
public void onTick(long millisUntilFinished) {}
public void onFinish() {
// INFLATE VIEW HERE
fragment = new Fragment2();
}
}.start();
break;
}
}
return fragment;
}
我不知道这是不是你在寻找,但我希望它会对你有所帮助:)。