我正在开发 android 平板电脑项目。有双窗格布局。左边有leftscrollfrg片段,右边也是执行,mainmapfragment,打开地图。在左侧,我单击 createproject 按钮,当我单击该按钮时,它会隐藏 mainmapfragment 并添加另一个片段,即 viewpager 片段。在第一页有 CreateProjectFrg1 片段,里面有添加另一个地图片段 (CreateProjectMapFrg) 的按钮。
问题是当第二张地图打开时,第一张地图显示为冻结。但我可以看到新打开的地图,因为旧地图是从左右填充的。而且当我关闭屏幕并重新打开时,冻结的旧地图消失了。
我怎么解决这个问题?
注意:在层次视图中,我看不到冻结的地图。
public class LeftScrollFrg extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View vw = inflater.inflate(R.layout.frg_left_behind, container, false);
frg_map = (MainMapFragment) getActivity()
.getSupportFragmentManager().findFragmentByTag(
MAP_FRAGMENT_TAG);
if (frg_map == null) {
frg_map = new MainMapFragment();
FragmentTransaction ft = getFragmentManager()
.beginTransaction();
ft.replace(R.id.above_framelayout, frg_map, MAP_FRAGMENT_TAG);
ft.addToBackStack(MAP_FRAGMENT_TAG);
ft.commit();
}
imgbtn_createproject.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
hideMainMapFrg();
openCreateProjectFrg();
}
});
imgbtn_map.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
hideCreateProjectFrg();
openMapFrg();
}
});
return vw;
}
private void hideMainMapFrg() {
fm = getActivity().getSupportFragmentManager();
frg_map = (MainMapFragment) fm
.findFragmentByTag(MAP_FRAGMENT_TAG);
if (fm != null) {
ft = fm.beginTransaction();
if (frg_map != null) {
if (frg_map.isVisible()) {
ft.hide(frg_map);
}
}
}
ft.commit();
}
private void openMapFrg() {
fm = getActivity().getSupportFragmentManager();
frg_map = (MainMapFragment) fm
.findFragmentByTag(MAP_FRAGMENT_TAG);
if (fm != null) {
ft = fm.beginTransaction();
if (frg_createproject != null) {
if (frg_createproject.isVisible()) {
ft.hide(frg_createproject);
}
}
if (frg_map == null) {
frg_map = new MainMapFragment();
ft.add(R.id.above_framelayout, frg_map, MAP_FRAGMENT_TAG);
} else {
if (frg_map.isHidden()) {
ft.show(frg_map);
}
}
ft.addToBackStack(MAP_FRAGMENT_TAG);
ft.commit();
}
}
}
public class MainMapFragment extends SupportMapFragment implements
LocationListener, LocationSource, OnInfoWindowClickListener,
OnMapClickListener {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
view = inflater.inflate(R.layout.frg_mainproject, container, false);
return view;
}
}
//frg_mainproject.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/rightpanel_margin" >
<fragment
android:id="@+id/main_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
/>
</RelativeLayout>
public class CreateProjectFrg1 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View vw = inflater.inflate(R.layout.frg_createproject1, container,
false);
imgvw_createprojectmap.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
openCreateMap();
}
});
return vw;
}
private void openCreateMap() {
fm = getActivity().getSupportFragmentManager();
frg_map = (CreateProjectMapFrg) fm
.findFragmentByTag(CREATEPROJECTMAP_FRAGMENT_TAG);
if (fm != null) {
ft = fm.beginTransaction();
if (frg_map == null) {
frg_map = new CreateProjectMapFrg();
ft.add(R.id.above_framelayout, frg_map, CREATEPROJECTMAP_FRAGMENT_TAG);
}
ft.addToBackStack(CREATEPROJECTMAP_FRAGMENT_TAG);
ft.commit();
}
}
}
public class CreateProjectMapFrg extends SupportMapFragment implements
OnMapClickListener, OnMarkerClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.frg_createprojectmap, container,
false);
} catch (InflateException e) {
}
setViewsResource(view);
setUpMapIfNeeded();
return view;
}
private void setUpMapIfNeeded() {
if (mMap == null) {
mMap = ((SupportMapFragment) getActivity()
.getSupportFragmentManager().findFragmentById(
R.id.frg_createproject_map)).getMap();
if (mMap != null) {
setUpMap();
}
}
}
}
//frg_createprojectmap.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/frg_createproject_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
更新代码