【发布时间】:2017-09-25 09:09:37
【问题描述】:
所以最近我正在研究片段。到目前为止,我所拥有的是一个包含三个按钮的栏(它更改了容器中的片段)。其中一个片段显示了谷歌地图。但是每当我更改片段时,谷歌地图的最后一个相机位置都没有保存。每次单击时都会初始化相机位置。即使我与其他片段来回移动,是否有任何解决方案可以保存我的最后一个片段数据和状态?我已经搜索了诸如显示和隐藏方法之类的东西,但我认为它不起作用:(
提前谢谢!
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;
}
}
【问题讨论】:
标签: android android-fragments save fragment