2017-09-25 58 views
0

所以最近我正在處理片段。到目前爲止我所擁有的是一個包含三個按鈕的欄(它改變了容器中的片段)。其中一個片段顯示Google Map。但是每當我更改片段時,Google地圖的最後一個攝像機位置都未保存。每次點擊時,相機位置都會被初始化。即使我與其他片段來回移動,是否有解決方案來保存我的最後片段數據和狀態?我搜索之類的顯示和隱藏方法,但我不認爲它的工作:(如何保存最後一個片段的數據

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); 

這裏有一個Fragment_googlemap!

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()一次:

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

感謝您的建議,但由於我是Android新手,我不知道要從我的代碼中觸摸什麼:(如果您有一點時間,可否請簡單看看我的Fragment_googlemap? – poream3387

+0

您的片段類看起來沒問題,當片段出現在屏幕上時,會調用'onCreateView()'類,這會導致佈局每次都「膨脹」(在屏幕上創建)。包裹在一個'if'語句中,以便它只被調用一次。 – kRiZ

0

你有一些選擇保存並傳遞您的數據。

  1. 接口 - 監聽從片段變量的變化,並將其保存到活動並通過意圖傳遞數據

  2. 緩存 - 靜態變量或數據存儲

  3. SharedPreference或數據庫

+0

其實我正在使用數據存儲進行緩存。鏈接是https://github.com/kimkevin/CachePot。謝謝! – kimkevin

+0

哦,存儲相機的位置也將與你的想法?順便說一句,謝謝你糾正我的可憐的語法:) – poream3387

+0

@ poream3387 yeap!我認爲你需要創建一個結果類,它有一個你想要保存的數據,比如位置或地圖信息。 – kimkevin

相關問題