Проблема SupportMapFragment во вложенном фрагменте TabHost
У меня есть 1FragmentActivity
1 основнойfragment
и 2tabshosted child fragment
один для просмотра списка другой для карты. У меня есть 2 проблемы здесь. Во-первых, я должен предотвращать воссоздание дочерних фрагментов на каждой вкладке. Вторая проблема; первый раз, когда я нажимаю вкладку для фрагмента карты карту можно увидеть. но возвращение к фрагменту списка и возврат к фрагменту карты карта не отображается. У меня действительно есть проблемы с этой частью в моем проекте. Мне нужен твой гид. Мой весь код, как показано ниже.
Основной (родительский) фрагмент
public class MainFragg extends Fragment implements OnTabChangeListener{
private FragmentTabHost mTabHost;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.mainfragment, container, false);
mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
mTabHost.setup(activity, getChildFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("feedlist").setIndicator("Fragment List"), FeedListFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("feedmap").setIndicator("Fragment Map"), FeedMapFragment.class, null);
mTabHost.setOnTabChangedListener(this);
return rootView;
}
public void onTabChanged(String tabId) {
}
}
Детский (список) фрагмент
public class FeedListFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_listfeed, container, false);
return rootView;
}
}
Детский фрагмент карты
public class FeedMapFragment extends SupportMapFragment implements LocationListener{
public FeedMapFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initilizeMap();
}
GoogleMap map;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment_feedmap, container, false);
if(map == null)
initilizeMap();
return rootView;
}
private void initilizeMap(){
SupportMapFragment mSupportMapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.mapwhere);
if (mSupportMapFragment == null) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
mSupportMapFragment = SupportMapFragment.newInstance();
fragmentTransaction.replace(R.id.mapwhere, mSupportMapFragment).commit();
}
if (mSupportMapFragment != null)
{
map = mSupportMapFragment.getMap();
if (map != null){
map.setMyLocationEnabled(true); // false to disable
map.getUiSettings().setZoomControlsEnabled(false); // true to enable
map.getUiSettings().setCompassEnabled(true);
map.getUiSettings().setMyLocationButtonEnabled(true);
LocationManager locationManager = (LocationManager) getActivity().getSystemService(getActivity().LOCATION_SERVICE);
locationManager.removeUpdates(this);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
if (provider == null)
onProviderDisabled(provider);
locationManager.requestLocationUpdates(provider, 0, 0, this);
Location location = locationManager.getLastKnownLocation(provider);
map.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude())));
map.animateCamera(CameraUpdateFactory.zoomTo(12));
}
}
}
}
Моя основная деятельность
public class MainActivity extends FragmentActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.mainactivity);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragContainer, new MainFragg(), "tabFrag");
}
}