2017-09-25 9 views
0

最近、私はフラグメントに取り組んでいます。これまで私が持っていたのは、3つのボタン(コンテナ内のフラグメントを変更する)を含むバーです。フラグメントの1つにGoogleマップが表示されます。しかし、フラグメントを変更するたびに、Google Mapの最後のカメラ位置は保存されませんでした。クリックするたびにカメラ位置が初期化されます。私が他の断片と行き来しても、私の最後の断片のデータと状態を保存する解決策はありますか? Fragment_googlemapを私はショーと非表示方法のようなものを検索したが、私はそれが事前に:(最後のフラグメントのデータを保存する方法

Thxをを働いているとは思わない!

private Toolbar toolbar; 
ImageButton btn_googleMap; 
ImageButton btn_localTimeLine; 
ImageButton btn_timeLine; 
ImageButton btn_myProfile; 
Fragment_googlemap fragment_googlemap = new Fragment_googlemap(); 
Fragment_localtimeline fragment_localtimeline = new Fragment_localtimeline(); 
Fragment_timeline fragment_timeline = new Fragment_timeline(); 

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

    toolbar = (Toolbar) findViewById(R.id.tool_bar); 
    setSupportActionBar(toolbar); 

    btn_localTimeLine = (ImageButton) findViewById(R.id.btn_localTimeLine); 
    btn_timeLine = (ImageButton) findViewById(R.id.btn_timeLine); 
    btn_googleMap = (ImageButton) findViewById(R.id.btn_mapTrending); 
    btn_myProfile = (ImageButton)findViewById(R.id.imgbtn_myProfile); 

    // Initialized fragment for Google Map 
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
    ft.add(R.id.container,new Fragment_googlemap()); 
    ft.commit(); 

    // Click listener for other fragments 
    View.OnClickListener listener = new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Fragment fragment = null; 
      if (v == findViewById(R.id.btn_mapTrending)) { 
       fragment = fragment_googlemap; 
      } else if (v == findViewById(R.id.btn_localTimeLine)) { 
       fragment = fragment_localtimeline; 
      } else if (v == findViewById(R.id.btn_timeLine)) { 
       fragment = fragment_timeline; 
      } 

      FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
      ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
      ft.replace(R.id.container, fragment); 
      ft.commit(); 
     } 
    }; 

    btn_googleMap.setOnClickListener(listener); 
    btn_localTimeLine.setOnClickListener(listener); 
    btn_timeLine.setOnClickListener(listener); 

をここだ!

public class Fragment_googlemap extends android.support.v4.app.Fragment 
implements OnMapReadyCallback{ 

View mRootView; 
private MapView mapView; 
private Spinner spinner_countries; 
private GoogleMap googleMap; 
FloatingActionButton floatingActionButton; 

public void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    if(mRootView==null){ 
     // Inflate the layout for this fragment 
     mRootView= inflater.inflate(R.layout.fragment_googlemap, container, false); 
    } 
    return mRootView; 
} 

@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 
    mapView = (MapView) view.findViewById(R.id.map); 
    mapView.onCreate(savedInstanceState); 
    mapView.onResume(); 
    mapView.getMapAsync(this);//when you already implement OnMapReadyCallback in your fragment 


    spinner_countries = (Spinner)view.findViewById(R.id.spinner_countries); 
    floatingActionButton = (FloatingActionButton)view.findViewById(R.id.fab); 
    floatingActionButton.setElevation(100); 

    floatingActionButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

     } 
    }); 
} 

@Override 
public void onMapReady(GoogleMap map) { 
    googleMap = map; 
} 

}

答えて

0

onClick()メソッドで毎回フラグメントの新しいインスタンスを作成していますが、新規作成ではなくクラスレベルでフラグメントのインスタンスを作成してみてくださいもの。

編集:ハックとして

、あなたのマップのフラグメントのため、あなたのルートビューのための変数を作成し、一度だけ、あなたのonCreateView()でのinflateメソッドを呼び出します。

class Fragment_googlemap { 
    ... 
    View mRootView; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     if (mRootView == null) { 
      mRootView = inflater.inflate(R.layout.fragment_googlemap, container, false); 
     } 

     return mRootView; 
    } 
} 
+0

アドバイスしたように上記のコードを変更しましたが、それでも最後の状態は保存されません。 _; – poream3387

+0

あなたのアドバイスをしてくれてありがとうございますが、私はアンドロイドを初めて使ったので、私のコードから何をすべきかわかりません:(少し時間があるのであれば、私のFragment_googlemapもまた簡単に見てください) – poream3387

+0

フラグメントのクラスは大丈夫です。onCreateView()クラスは、フラグメントが画面上に来るときに呼び出され、そのたびにレイアウトが「膨らんだ」(画面上に作成される)ようになります。 – kRiZ

0

あなたはいくつかのオプションを持っていますあなたのデータを保存して渡す。

  1. インタフェース - 断片からの変数の変化を聞くとアクティビティにそれを保存し、意図

  2. キャッシュを介してデータを渡す - 静的変数またはデータストア

  3. SharedPreferenceまたはデータベース

+0

実際には私が作ったキャッシングのためにデータストアを使っています。リンクはhttps://github.com/kimkevin/CachePotです。ありがとうございました! – kimkevin

+0

ああ、カメラの位置を保存してもアイデアと一緒に働くだろうか?私の貧しい文法を訂正してくれてありがとう:) – poream3387

+0

@ poream3387 yeap!私はあなたが場所や地図情報のように保存したいデータを持っている結果のためのクラスを作成する必要があると思います。 – kimkevin

関連する問題