Google Map Fragment overlays another fragment

2019-08-12 22:08发布

Basing on the code given here my app implements the Google Maps V2. The problem is that I have this fragment-based menu (sorry if I misspelled the concept) that gets overlayed by the Google Maps fragment after a very short time.

Is it possible to make the Google Maps fragment to stay in the back of the menu fragment?

Map code:

public class AdMapFragment extends Fragment
{

MapView mMapView;
private GoogleMap googleMap;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // inflat and return the layout
    View v = inflater.inflate(R.layout.activity_ad_map, container,
            false);
    mMapView = (MapView) v.findViewById(R.id.mapView);
    mMapView.onCreate(savedInstanceState);

    mMapView.onResume();// needed to get the map to display immediately

    try {
        MapsInitializer.initialize(getActivity().getApplicationContext());
    } catch (Exception e) {
        e.printStackTrace();
    }

    googleMap = mMapView.getMap();
    // latitude and longitude
    double latitude = 17.385044;
    double longitude = 78.486671;

    // create marker
    MarkerOptions marker = new MarkerOptions().position(
            new LatLng(latitude, longitude)).title("Hello Maps");

    // Changing marker icon
    marker.icon(BitmapDescriptorFactory
            .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));

    // adding marker
    googleMap.addMarker(marker);
    CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(new LatLng(17.385044, 78.486671)).zoom(12).build();
    googleMap.animateCamera(CameraUpdateFactory
            .newCameraPosition(cameraPosition));

    return v;
}

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

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

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

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

}

I made activities to work this way - they are just extending the MenuActivity so there is only one central activity for menu. Activity code

public class AdMapActivity extends MenuActivity
{
}

MenuActivity refers to caller class and aggregates the request. Menu code

public class MenuActivity extends Activity
{

private DrawerLayout menuLayout;
private ListView menuList;
private ActionBarDrawerToggle menuToggle;
private ArrayList<MenuListItem> menuItems;
private MenuListItemAdapter menuAdapter;

private ArrayList<BasicNameValuePair> fragmentsMenu;
private ArrayList<String> fragmentsMenuClass;
private ArrayList<String> fragmentsMenuName;
private ArrayList<Integer> fragmentsMenuIcon;
private HashMap<String, String> fragmentsIndividual;

private void displayView(int position)
{
    String className = fragmentsMenuClass.get(position);

    try
    {
        Fragment fragment = (Fragment) Class.forName(getPackageName() + '.' + className.substring(0, className.length() - 8) + "Fragment").newInstance();

        if (fragment != null)
        {
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();

            menuList.setItemChecked(position, true);
            menuList.setSelection(position);
            menuLayout.closeDrawer(menuList);
        }
    }
    catch (Exception e)
    {
        Log.e("error", Log.getStackTraceString(e));
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu_list);

    fragmentsMenuClass = new ArrayList<String>();
    fragmentsMenuClass.add("AdListActivity");
    fragmentsMenuClass.add("BlogListActivity");
    fragmentsMenuClass.add("InterestListActivity");
    fragmentsMenuClass.add("BrandListActivity");
    fragmentsMenuClass.add("ProfileActivity");
    fragmentsMenuClass.add("PaymentsActivity");
    fragmentsMenuClass.add("AdMapActivity");
    fragmentsMenuClass.add("ContactsActivity");

    fragmentsMenuName = new ArrayList<String>();
    fragmentsMenuName.add(getString(R.string.menu_item_ad_list));
    fragmentsMenuName.add(getString(R.string.menu_item_blog_list));
    fragmentsMenuName.add(getString(R.string.menu_item_interest_list));
    fragmentsMenuName.add(getString(R.string.menu_item_brand_list));
    fragmentsMenuName.add(getString(R.string.menu_item_profile));
    fragmentsMenuName.add(getString(R.string.menu_item_payments));
    fragmentsMenuName.add(getString(R.string.menu_item_ad_map));
    fragmentsMenuName.add(getString(R.string.menu_item_contacts));

    fragmentsMenuIcon = new ArrayList<Integer>();
    fragmentsMenuIcon.add(R.drawable.global_button_like_image);
    fragmentsMenuIcon.add(R.drawable.global_button_like_image);
    fragmentsMenuIcon.add(R.drawable.global_button_like_image);
    fragmentsMenuIcon.add(R.drawable.global_button_like_image);
    fragmentsMenuIcon.add(R.drawable.global_button_like_image);
    fragmentsMenuIcon.add(R.drawable.global_button_like_image);
    fragmentsMenuIcon.add(R.drawable.global_button_like_image);
    fragmentsMenuIcon.add(R.drawable.global_button_like_image);

    fragmentsMenu = new ArrayList<>();

    menuItems = new ArrayList<>();

    for (int i = 0; i < fragmentsMenuClass.size(); i++)
    {
        menuItems.add(new MenuListItem(String.valueOf(fragmentsMenuName.get(i)), (Integer) fragmentsMenuIcon.get(i)));
    }

    menuList = (ListView) findViewById(R.id.menu_list);

    menuList.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            displayView(position);
        }
    });

    menuAdapter = new MenuListItemAdapter(getApplicationContext(), menuItems);

    menuList.setAdapter(menuAdapter);

    menuLayout = (DrawerLayout) findViewById(R.id.menu_layout);

    menuToggle = new ActionBarDrawerToggle(this, menuLayout, R.drawable.global_button_like_image, R.string.app_name, R.string.app_name)
    {
        public void onDrawerClosed(View view)
        {
            invalidateOptionsMenu();
        }

        public void onDrawerOpened(View drawerView)
        {
            invalidateOptionsMenu();
        }
    };

    menuLayout.setDrawerListener(menuToggle);

    final String className = getClass().getSimpleName();

    if (savedInstanceState == null)
    {
        if (fragmentsMenuClass.contains(className))
        {
            displayView(0);
        }
        else
        {
            try
            {
                Fragment fragment = (Fragment) Class.forName(getPackageName() + '.' + className.substring(0, className.length() - 8) + "Fragment").newInstance();

                FragmentManager fragmentManager = getFragmentManager();
                fragment.getView().bringToFront();
                fragmentManager.beginTransaction().add(R.id.frame_container, fragment).commit(); //replace -> add
            }
            catch (Exception e)
            {
                Log.e("error", Log.getStackTraceString(e));
            }
        }
    }
}

}

Menu layout

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menu_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
    android:id="@+id/frame_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<ListView
    android:id="@+id/menu_list"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@color/adListItemGray"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/menu_list_item_selector"
    android:background="#FFF"/>

</android.support.v4.widget.DrawerLayout>

1条回答
一纸荒年 Trace。
2楼-- · 2019-08-12 22:38

As we discussed above, add the FrameLayout resolved the problem.

For more details, please refer to here

查看更多
登录 后发表回答