1

私は次の状況にあります:

  • 私はAndroidアクションバーを使用して3つのタブを持つシンプルなアプリを持っています
  • 最初のタブ(タブではなく、下のGUI)にマップが含まれている必要があります

アクティビティ、フラグメント(Java)、およびフラグメント(xml)を使用したセットアップがどのようになるかはよくわかりません。google playservicesから取得するマップの例はすべて、アクティビティを使用するだけです。

アクションバー(タブナビゲーション)を操作するときは、フラグメントを使用する必要があります。そうですか?

だから私は正しく動作しない次のコンポーネントを実装しています。

package ch.bfh.femtho.skiguide;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends Activity {

public static Context appContext;

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

    appContext = getApplicationContext();

    // Set up the action bar to show tabs.
    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // For each of the sections in the app, add a tab to the action bar.

    ActionBar.Tab mapTab = actionBar.newTab().setText(R.string.tab_title_map);
    ActionBar.Tab poiListeTab = actionBar.newTab().setText(R.string.tab_title_poiliste);
    ActionBar.Tab meetingpointTab =    actionBar.newTab().setText(R.string.tab_title_meetingpoint);


    mapTab.setTabListener(new TabListener<MyMapFragment>(
            this, "map", MyMapFragment.class));
    poiListeTab.setTabListener(new TabListener<PoiListeFragment>(
            this, "poiliste", PoiListeFragment.class));
    meetingpointTab.setTabListener(new TabListener<MeetingpointFragment>(
            this, "meetingpoint", MeetingpointFragment.class));

    actionBar.addTab(mapTab);
    actionBar.addTab(poiListeTab);
    actionBar.addTab(meetingpointTab);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
}

}

TabListener:

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;

public class TabListener<T extends Fragment> implements ActionBar.TabListener {
    private Fragment mFragment;
    private final Activity mActivity;
    private final String mTag;
    private final Class<T> mClass;

    /** Constructor used each time a new tab is created.
      * @param activity  The host Activity, used to instantiate the fragment
      * @param tag  The identifier tag for the fragment
      * @param clz  The fragment's Class, used to instantiate the fragment
      */
    public TabListener(Activity activity, String tag, Class<T> clz) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
    }

    /* The following are each of the ActionBar.TabListener callbacks */

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // Check if the fragment is already initialized
        if (mFragment == null) {
            // If not, instantiate and add it to the activity
            mFragment = Fragment.instantiate(mActivity, mClass.getName());
            ft.add(android.R.id.content, mFragment, mTag);
        } else {
            // If it exists, simply attach it in order to show it
            ft.attach(mFragment);
        }
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        if (mFragment != null) {
            // Detach the fragment, because another one is being attached
            ft.detach(mFragment);
        }
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // User selected the already selected tab. Usually do nothing.
    }
}

断片:

import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

public class MyMapFragment extends Fragment {

private MapView mMapView;
private GoogleMap mMap;
private Bundle mBundle;
private UiSettings mUiSettings;

int mCurCheckPosition = 0;

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("curChoice", mCurCheckPosition);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (savedInstanceState != null) {
            // Restore last state for checked position.
        mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
    }
}

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

    try {
        MapsInitializer.initialize(getActivity());
    } catch (GooglePlayServicesNotAvailableException e) {
        // TODO handle this situation
    }

    mMapView = (MapView) inflatedView.findViewById(R.id.map);
    mMapView.onCreate(mBundle);
    setUpMapIfNeeded(inflatedView);

    return inflatedView;
}



@Override
public void onDestroyView() {
    super.onDestroyView();
    MyMapFragment f = (MyMapFragment) getFragmentManager().findFragmentById(R.id.map);
    if (f != null) {
        getFragmentManager().beginTransaction().remove(f).commit();
    }
}



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mBundle = savedInstanceState;
}

private void setUpMapIfNeeded(View inflatedView) {
    if (mMap == null) {
        mMap = ((MapView) inflatedView.findViewById(R.id.map)).getMap();
        if (mMap != null) {
            setUpMap();
        }
    }
}

private void setUpMap() {
    mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    mMap.setOnMapClickListener(this);
    mMap.setOnMapLongClickListener(this);
    mMap.setMyLocationEnabled(true);
    mUiSettings = mMap.getUiSettings();
    mUiSettings.setMyLocationButtonEnabled(true);
}

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

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

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

}

フラグメント(xml):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
   tools:context=".MainActivity" >

   <fragment
       android:id="@+id/map"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       class="com.google.android.gms.maps.MapFragment" />

</RelativeLayout>

別のタブに移動してからマップに戻ると、問題が発生します。追加されたマーカーが消え、カメラの位置が失われ、...

誰かがこれを手伝ってくれますか?

ありがとう

4

1 に答える 1

0

mapFragmentのonSaveInstanceStateメソッドをオーバーライドし、必要なすべてのデータをsavedInstantStateオブジェクト(保存しているintだけでなく、作成した場所とマーカーの場所を含む)に保存し、onCreateメソッドでこのすべてのデータを再利用する必要があります。

状態の保存の詳細については、次を参照してください。

http://www.eigo.co.uk/labs/managing-state-in-an-android-activity/

于 2013-03-01T20:28:26.350 に答える