从另一个片段

时间:2015-05-26 18:34:56

标签: java android android-fragments

我在主要活动中使用导航抽屉。主要活动保存地图片段,并在导航抽屉上选择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"/>

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

1 个答案:

答案 0 :(得分:1)

您正在使用SettingsFragment替换MapFragment,并且(截至目前)无法返回。

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

顺便说一下。难道 d _frame只是一个错字?在您的布局文件中,它表示包含 t _frame。

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

检查this answer如何使用MapView。