2017-09-25 59 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

相关问题