活动开始时,ViewPager不会加载内部片段

时间:2013-11-28 07:09:59

标签: android android-fragments android-viewpager android-tabs

我有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);
            }   
        }   
    }
}

1 个答案:

答案 0 :(得分:1)

我认为这里的问题是在viewPager中使用map片段,你可以用任何其他片段替换它并再试一次。

如果问题是在viewPager

中使用map片段,请检查此link