我有activity
个标签(包括viewpager
),每个标签都包含activity
底部带有viewpager的片段。当我的activity
首次启动时,第0个标签会加载并显示地图(在fragment
内),但viewpager
不会加载。当我切换标签并再次返回地图fragment
时,它会加载viewpager
。为什么我的地图片段viewpager
无法在第一时间加载,我缺少的是什么。请帮忙。
MainActivity
上课
public class MainActivity extends FragmentActivity implements
ActionBar.TabListene, GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener {
static GoogleMap map;
static ViewPager mMap, mList,
mSetting;
ImagePAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayHomeAsUpEnabled(true);
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
mAdapter = new ZAdapter(getApplicationContext(), mList);
mViewPager.setAdapter(mSectionsPagerAdapter);
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab().setText(listTab[i]).setTabListener(this));
}
}catch(Exception e){
Logger.writeLOG(LOG_TAG, "@onCreate ", e);
}
}
}
SectionsPagerAdapter
上课
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int position) {
// TODO Auto-generated method stub
switch (position) {
case 0:
return new MapFragment();
case 1:
return new ListFragment();
case 2:
return new SettingFragment();
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return listTab.length;
}
}
ListFragment
public class ListFragment extends Fragment {
View rootView = null;
public Fragment newInstance() {
return new ListFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (null == savedInstanceState)rootView = inflater.inflate(R.layout.listfragment, container,false);
ViewPager mPager = (ViewPager) rootView.findViewById(R.id.pagerAddList);
MainActivity.mList = mPager ;
mPager .setAdapter(mAdapter);
mPager .setCurrentItem(0);
}
}
MapFragment
上课
public class MapFragment extends Fragment {
GoogleMap map;
public Fragment newInstance() {
return new MapFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
try{
Log.e("", "onCreateView");
if (null == savedInstanceState) rootView = inflater.inflate(R.layout.lay_map, container, false);
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
MainActivity.map = map;
ViewPager mPager = (ViewPager) rootView.findViewById(R.id.pagerAddMap);
MainActivity.mMap = mPager;
mPager.setAdapter(mAdvertisementAdapter);
mPager.setCurrentItem(0);
}
}
}
SettingFragment
上课
public class SettingFragment extends Fragment {
View rootView = null;
public Fragment newInstance() {
return new SettingFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
try{
if (null == savedInstanceState)rootView = inflater.inflate(R.layout.settinglayout, container,false);
ViewPager mPager = (ViewPager) rootView.findViewById(R.id.pagerAddSetting);
MainActivity.mSetting = mPager;
mPager.setAdapter(mAdvertisementAdapter);
mPager.setCurrentItem(0);
}
@Override
public void onResultsSucceeded(String result) {
mAdapter = new ImagePAdapter(listUrls);
mLocatorGlobal.setListUrls(listUrls);
if (mList != null) {
mList.setAdapter(mAdapter);
}
if (mSetting != null) {
mSetting.setAdapter(mAdapter);
}
if (mMap != null) {
mMap.setAdapter(mAdapter);
}
}
}
}
答案 0 :(得分:1)