SupportMapFragment-Problem in verschachteltem TabHost-Fragment
Ich habe 1FragmentActivity
1 mainfragment
und 2tabshosted child fragment
eins ist für Listview ein anderes für Karte. Ich habe hier 2 Probleme. Erstens muss ich verhindern, dass bei jeder Änderung untergeordnete Fragmente neu erstellt werden. Zweites Problem; Das erste Mal, wenn ich auf die Registerkarte für Kartenfragmente klicke, ist eine Karte zu sehen. Es wird jedoch nicht angezeigt, zur Fragmentliste zurückzukehren und zur Kartenfragmentkarte zurückzukehren. Ich habe wirklich Probleme mit diesem Teil in meinem Projekt. Ich brauche deinen Führer. Mein aller Code ist wie folgt.
Main (Parent) Fragment
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) {
}
}
Child (list) fragment
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;
}
}
Child (map) fragment
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));
}
}
}
}
Meine Hauptaktivität
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");
}
}