【发布时间】:2013-12-31 16:08:09
【问题描述】:
我正在使用 ViewPager 和三个 Fragment。此片段之一 (MapFragment) 使用 google maps Api v2。
public class ViewPagerAdapter extends FragmentStatePagerAdapter{
public static final int NUM_PAGES = 3;
public static final int [] titles = {R.string.map, R.string.bus_stop_favorites, R.string.bus_lines};
private Context context;
public ViewPagerAdapter(Context context, FragmentManager fragmentManager) {
super(fragmentManager);
this.context = context;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new MapFragment();
case 1:
return new FavoritesFragment();
case 2:
return new LinesFragment();
default:
throw new IllegalArgumentException("La posicion fue " + position + " y deberia ser menor a " + NUM_PAGES);
}
}
@Override
public int getCount() {
return NUM_PAGES;
}
@Override
public CharSequence getPageTitle(int position) {
return context.getString(titles[position]);
}
}
当我运行我的应用程序时,我将片段 1 (FavoriteFragment) 设置为默认值,所以它是我看到的第一个。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.application_tus);
initActionBar();
viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new ViewPagerAdapter(this, getSupportFragmentManager()));
TitlePageIndicator titleIndicator = (TitlePageIndicator)findViewById(R.id.titles_viewpager);
titleIndicator.setViewPager(viewPager,1);
}
}
MapFragment 是 FavoriteFragment 的兄弟,所以它在我的应用启动并且 Google Map API 请求完成时加载。但是 Google Maps API 有配额限制,所以我喜欢 MapFragment 仅在可见时才执行 Google Map API 请求,如果用户不向左滑动,则 MapFragment 不可见并且任何请求都已完成。
这张图片显示了 ViewPager 上的三个片段
【问题讨论】:
标签: android android-fragments google-maps-android-api-2 supportmapfragment