FragmentActivity与Fragment类型的GoogleMap交互

时间:2019-01-30 09:25:37

标签: java android android-fragments android-intent

问题,FragmentActivity(称为A)如何与另一个Fragment(称为B)交互,并且B依赖Fragment(称为C),但是C尚未被告知做B。

假设,C尚未具有OnButtonClick(),因此B尚未被转换到,但是A拥有了OnButtonClick(),因此现在A和B可以进行通信了。

场景, A是一些具有按钮的视图,该按钮可以随机添加,更新,删除,修改B中的google map。  C是一些具有按钮的视图,该按钮可以转换到google map视图,并且该视图可以反映B的动态更新。

A的流,A中的OnButtonClick()与 B 进行交互。

C的流,C中的OnButtonClick(),过渡到 B

public class A extends FragmentActivity implements OnClickListener {
 @Override
 public void onClick(View v) {
    onButtonClick(/*interact with B*/);
 }
}

public class C extends Fragment implements OnClickListener {
 @Override
 public void onClick(View v) {
    onButtonClick(/*transition to B*/);
    FragmentManager fragmentManager = this.getFragmentManager();
    FragmentTransaction fragmentTransaction = 
     fragmentManager.beginTransaction();
    Fragment map = new B();
    fragmentTransaction.replace(R.id.layout_of_C, layout_of_B);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
 }
}

public class B extends Fragment implements OnMapReadyCallback {
 @Override public void onMapReady(GoogleMap map){
  ...some map stuff...
 }
}

abstract view

1 个答案:

答案 0 :(得分:0)

通过执行以下操作找到解决方案:

(1)实现MapIf接口,MapLoader是我的片段,其中包含共享的Google地图,

(2)在顶级活动中实例化我的共享Google Map对象,

(3)在顶层活动中实现地图界面,

(4)在顶级活动中的吸气剂,

(5)在片段B上实现MapIf,

注意:关键是将片段B中的上下文强制转换为通过getter访问我的对象的顶级活动。

/*
 getMapLoaderSet: need a set with capacity 1, unique map
 getMapLoader: unique map to be accessed
 onMapLoaderSet: do some UI msg Toast on data
*/
interface MapIf<MapLoader>{
 Set<MapLoader> getMapLoaderSet(); 
 MapLoader getMapLoader();
 void onMapLoaderSet(Set<MapLoader> s);
}

public class MainActivity (Top Level) extends Activity{
 private MapLoader mMapLoader;
 private MapIf<MapLoader> mMapIf;
 @Override public void onCreate(...){
 /*unique set of 1; only 1 google map per application*/
 final int capacity = 1;
 this.mMapLoaderSet = new HashSet<>(capacity);
 mMapLoader = new MapLoader();
 mMapLoaderSet.add(mMapLoader);

 /*interface to share map accessibility*/
 mMapIf = new MapIf<MapLoader>() {
 @Override public Set<MapLoader> getMapLoaderSet() { return mMapLoaderSet; }
 @Override public MapLoader getMapLoader() { return mMapLoader; }
 @Override public void onMapLoaderSet(Set<MapLoader> s) { }
 };//mMapIf
 mMapIf.onMapLoaderSet(mMapLoaderSet);
}

public class SomeView (A) extends FragmentActivity implements OnClickListener, MapIf<MapLoader> {
 private Set<MapLoader> mMapLoaderSet;
 private MapLoader mMapLoader;
 @Override for OnCreate(...)
  mMapLoaderSet = ((MainActivity)context).getmMapLoaderSet();
  mMapLoader = mMapLoaderSet.iterator().next();
 }
 @Override for OnClickListener
 @Override for MapIf<MapLoader>
 @Override public Set<MapLoader> getMapLoaderSet() {
  return ((MainActivity)this.getApplicationContext()).getmMapLoaderSet();
 }
 @Override public MapLoader getMapLoader() {
  return ((MainActivity)this.getApplicationContext()).getmMapLoader();
 }
 @Override public void onMapLoaderSet(Set<MapLoader> s) { }
 }

public class MapLoader (B) extends Fragment implements 
 OnMapReadyCallback, MapIf<MapLoader> {
 /* same class members as SomeView (A)*/
 @Override for OnCreate(...)
 @Override for OnMapReadyCallback
 @Override for MapIf<MapLoader>
 /* same override implementation as SomeView (A)*/
 /* use getContext() instead of getApplicationContext()*/
}