第二次滑动选项卡无法正确加载

时间:2015-04-08 00:28:18

标签: android google-maps pagerslidingtabstrip

我正在使用SlidingTabStrip.with片段中的Google SlidingTabLayout。 片段主机2其他片段,一个具有地图视图,另一个具有列表视图。 如果我是第一次从导航抽屉中调用此片段(打开应用程序),它会正确显示带有2个标签的片段。

但是当我尝试通过导航抽屉或后退按钮返回时,片段不会加载任何内容,只有像slidelayout这样的灰色背景没有正确加载。

任何人都可以指出我做错了什么吗?

StoreTabHostFragment.java

public class StoreTabHostFragment extends Fragment {
    private ViewPager viewPager;
    private StoresPagerAdapter mAdapter;
    private SlidingTabLayout tabLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_stores, container, false);
        viewPager = (ViewPager) rootView.findViewById(R.id.pager);
        mAdapter = new StoresPagerAdapter(getActivity().getSupportFragmentManager(), getActivity());
        viewPager.setAdapter(mAdapter);
        tabLayout = (SlidingTabLayout) rootView.findViewById(R.id.sliding_tabs);
        tabLayout.setDistributeEvenly(true);
        tabLayout.setViewPager(viewPager);

        return rootView;
    }

}

StoreMapFragment.java(省略了一些功能)

public class StoreMapFragment extends Fragment implements LocationListener,OnMapReadyCallback {
    private GoogleMap mMap;
    private MapView mMapView;
    private Bundle mBundle = new Bundle();
    private Double currentLng = -99.274643;
    private Double currentLat = 19.360460;
    private LocationManager locationManager;
    private String provider;
    private CameraPosition cameraPosition;
    private SharedPreferences sharedPreferences;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBundle = savedInstanceState;
        sharedPreferences = getActivity().getSharedPreferences(Constants.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_store_map, container, false);
        try{
            MapsInitializer.initialize(getActivity());
        }catch(Exception e){
            e.printStackTrace();
        }
        mMapView = (MapView) rootView.findViewById(R.id.map);
        mMapView.onCreate(mBundle);
        mMapView.getMapAsync(this);
        //setUpMapIfNeeded(rootView);
        return rootView;
    }
    private void addMarker(GoogleMap map, MFinderStore store){
        map.addMarker(new MarkerOptions().position(new LatLng(Double.valueOf(store.lat), Double.valueOf(store.lng)))
                .title(store.storeName)
                .snippet(store.storeAddress)
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.map_marker)));
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        googleMap.addMarker(new MarkerOptions().position(new LatLng(currentLat, currentLng)).title(getString(R.string.your_position)));
        cameraPosition = new CameraPosition.Builder().target(new LatLng(currentLat, currentLng)).zoom(14).build();
        googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
        getStoreList(googleMap);
    }

    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mMapView.onPause();
        //cameraPosition = mMap.getCameraPosition();
    }

    @Override
    public void onDestroy() {
        mMapView.onDestroy();
        super.onDestroy();
    }

}

StorePagerAdapter.java

public class StoresPagerAdapter extends FragmentPagerAdapter {
    private static int FRAGMENT_NUMBER = 2;
    private Context context;
    private String tabTitles[] = new String[] {"Mapa", "Sucursales"};

    public StoresPagerAdapter(FragmentManager fm, Context context){
        super(fm);
        this.context = context;
    }
    @Override
    public Fragment getItem(int index){
        switch(index){
            case 0:
                return new StoreMapFragment();
            case 1:
                return new StoreListFragment();
        }
        return null;
    }
    @Override
    public int getCount() {
        // get item count - equal to number of tabs
        return FRAGMENT_NUMBER;
    }
    @Override
    public CharSequence getPageTitle(int position){
        return tabTitles[position];
    }

}

1 个答案:

答案 0 :(得分:2)

使用FragmentStatePagerAdapter而不是使用FragmentPagerAdapter来解决这个问题