我在main活动中使用导航抽屉。main活动包含地图片段,并在导航抽屉中选择SettingsFragment时应显示片段。片段会显示出来,但是当我从抽屉中选择另一个项目时地图片段不会显示显示时,仅显示设置片段的布局。

main_activity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

}

private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        selectItem(position);
    }
}

private void selectItem(int position) {

    Fragment fragment = null;

    switch (position) {
        case 0:
            Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
            if (location == null)
              LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
            else
              handleNewLocation(location);
            break;
        case 1:
              output_locations(dataFromAsyncTask);
            break;
        case 2:
            /*fragment is called here */
            fragment = new SettingsFragment();
            break;
       default:
           break;
       }

    if (fragment != null) {
        Bundle data = new Bundle();
        fragment.setArguments(data);
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(osArray[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
        }
    else{
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(osArray[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    }

}


activity_main.xml

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

<!-- The main content view -->
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/content_frame">
    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment"
        tools:layout="@layout/activity_main" />
</FrameLayout>

<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#fff"/>




SettingsFragment.java类

public class SettingsFragment extends Fragment {

public SettingsFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View Settings_view = inflater.inflate(R.layout.settings_fragment, container, false);

    return Settings_view;
}
}


settings_fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@color/background_material_light">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="40px"
    android:textColor="#ffffff"
    android:layout_gravity="center"
    android:id="@+id/detail"/>




试图解决这个问题几天...

最佳答案

您正在用SettingsFragment替换MapFragment,并且(到目前为止)它无法恢复。

fragmentManager.beginTransaction().replace(R.id.contend_frame, fragment).commit();


顺便说一句。是contend_frame的错字?在您的布局文件中,显示为content_frame。

我的建议是将MapFragment(使用MapView)放到自己的布局中,将Fragment-class放到activity_main中,并在启动时加载该片段。
因此,您可以在需要时再次显示它。 (例如,通过popBackStack或用它替换SettingsFragment)。

检查this answer如何使用MapView。

10-06 14:55