我正在创建一个应用程序,我想在我的CustomMapFragment
和ListFragment
之间切换。如果我启动应用程序并调用replaceFragment
:
private void replaceFragment(final Fragment fragment) {
final FragmentManager fragmentManager = mActivity.getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.main_content_frame, fragment)
.commit();
}
使用
replaceFragment(new CustomMapFragment())
时,Map
看起来应该是这样。CustomMapFragment
是一个片段,在这里我用原生的FrameLayout
替换aMapFragment
,使其与代码分离。如果我在button click上再次调用replace方法,或者使用不同的
Fragment
(例如ListFragment
)调用其他方法,那么FrameLayout
将被替换为这个,一切都很好。如果我现在再次尝试用
CustomMapFragment
替换它,就像在开始时一样,加载了Map
,但它缺少在Marker
中所做的每一个CustomMapFragment
和每一个设置。CustomMapFragment
public class CustomMapFragment extends BaseFragment {
private static final String LOG_TAG = PosMapFragment.class.getSimpleName();
private MapFragment mMapFragment;
private GoogleMap mMap;
private ImageLoader mImageLoader;
private Context mContext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = getActivity();
final RequestQueue reqQueue = Volley.newRequestQueue(mContext);
mImageLoader = new ImageLoader(reqQueue, new BitmapLruCache(mContext));
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.maps_fragment, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mMapFragment = MapFragment.newInstance();
replaceFragment(mMapFragment);
}
private void postMapInitialisation() {
new View(getActivity()).post(new Runnable() {
@Override
public void run() {
addTestMarker(0, 0, "http://www.jimis-cyberstore.com/store/media/gp_batman_tshirts.jpg");
addTestMarker(10, 10, "http://img.netzwelt.de/software/icons/2012/8842/android-sdk.png");
addTestMarker(50, 50, "http://fs02.androidpit.info/ali/x84/4053084-1374511678988-80x80.png");
addTestMarker(5, 0, "http://im.wk.io/images/77e3e40/kitkat-chromium-ubernimmt-in-android.jpeg");
addTestMarker(0, 10, "http://www.mchme.de/cms/kategorien/18_Software/dateien/fb_logo_80.jpg");
addTestMarker(15, 5, "http://navigator.ptvgroup.com/uploads/pics/PTV_Navigator_Android_Windows2_80x80px_01.png");
}
});
}
@Override
public void onResume() {
super.onResume();
setUpMapIfNeeded();
postMapInitialisation();
EventBusProvider.getInstance().register(this);
}
private void replaceFragment(final Fragment fragment) {
final FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.maps, fragment)
.commit();
}
private void setUpMapIfNeeded() {
if (mMap == null) {
mMap = mMapFragment.getMap();
if (mMap != null) {
mMap.setMyLocationEnabled(true);
mMap.setInfoWindowAdapter(this);
mMap.setOnInfoWindowClickListener(this);
}
}
}
private void addTestMarker(final double lat, final double lng, String url){
if(mMap != null){
final NetworkImageMarker networkImageMarker = new NetworkImageMarker(mContext, url);
final Marker marker = mMap.addMarker(networkImageMarker.getMarkerOptions(lat, lng, mImageLoader));
networkImageMarker.setMarker(marker);
networkImageMarker.setReloadListener(new OnReloadListener() {
@Override
public void reAddNetworkImageMarker(Bitmap bitmap) {
mMap.addMarker(networkImageMarker.getMarkerOptions(lat, lng, bitmap));
}
});
}
}
@Subscribe
public void onLocationChanged(final LocationChangedEvent event) {
centerMap(event.mLocation);
}
private void centerMap(Location location){
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
centerMapAtLocation(latLng);
}
private void centerMapAtLocation(LatLng location) {
mMap.moveCamera(CameraUpdateFactory.newLatLng(location));
mMap.animateCamera(CameraUpdateFactory.zoomTo(14));
}
}
主布局只是一个
FrameLayout
,它会被不同的Fragment
s替换。我也试着只加载一个Fragment
,其中CustomMapFragment
和ListFragment
同时加载,两个FrameLayout
s,因此它们相互堆叠,但结果是相同的行为。似乎
MapFragment
有问题(而不是SupportMapFragment
)。开始时加载
MapFragment
看起来很完美MapFragment
>MapFragment
>ListFragment
=MapFragment
是一个空映射MapFragment
>ListFragment
=MapFragment
是一个空映射MapFragment
>MapFragment
=MapFragment
是一个空映射 最佳答案
因为您试图替换另一个片段中的片段,所以应该在replace方法中使用getChildFragmentManager()而不是getFragmentManager()。
private void replaceFragment(final Fragment fragment) {
final FragmentManager fragmentManager = getChildFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.maps, fragment)
.commit();
}
不幸的是,getchildfragmentmanager()只支持api lvl>=17,因此您可能需要使用android-support-v4库。