我尝试使用回调来在我的片段之间进行通信,但似乎infowindowfragment无法识别我的mapFragment作为其父片段,我可以做些什么工作或者有其他方法来做这件事?
Mapfragment(parentFragment):
public class MapFragment extends Fragment implements MapView, OnMapReadyCallback, //GoogleMap.InfoWindowAdapter,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener
,InfoWindowFragment.OnChildFragmentInteractionListener{
...
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mapInfoWindowFragment =
(MapInfoWindowFragment) getChildFragmentManager().findFragmentById(R.id.map);
mapInfoWindowFragment.getMapAsync(this);
}
...
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
setOnMakerClick(map);
moveCameraToLastKnowLocation();
}
...
public void setOnMakerClick(final GoogleMap googleMap){
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
NearDriver nearDriver = markers.get(marker);
LatLng position = new LatLng(nearDriver.getLatitude()+0.007, nearDriver.getLongitude());
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 15));
//marker.showInfoWindow();
final int offsetX = (int) getResources().getDimension(R.dimen.marker_offset_x);
final int offsetY = (int) getResources().getDimension(R.dimen.marker_offset_y);
final InfoWindow.MarkerSpecification markerSpec =
new InfoWindow.MarkerSpecification(offsetX, offsetY);
InfoWindowFragment infoWindowFragment = new InfoWindowFragment();
final InfoWindow infoWindow = new InfoWindow(marker, markerSpec, infoWindowFragment);
mapInfoWindowFragment.infoWindowManager().toggle(infoWindow, true);
infoWindowFragment.render(nearDriver);
return true;
}
});
}
...
@Override
public void messageFromChildToParent(Place place) {
Log.d("d", "MapFragment - Place: " + place.getName());
setOnMakerClick(map);
}
InfoWindowFragment(“ChildFragment”):
public interface OnChildFragmentInteractionListener {
void messageFromChildToParent(Place place);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
// check if parent Fragment implements listener
if (getParentFragment() instanceof OnChildFragmentInteractionListener) {
mParentListener = (OnChildFragmentInteractionListener) getParentFragment();
} else {
throw new RuntimeException("The parent fragment must implement OnChildFragmentInteractionListener");
}
}
logcat的:
06-13 00:53:57.427 18791-18791/com.rsm.yuri.projecttaxilivre E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.rsm.yuri.projecttaxilivre, PID: 18791
java.lang.RuntimeException: The parent fragment must implement OnChildFragmentInteractionListener
at com.rsm.yuri.projecttaxilivre.map.InteractiveInfoWindow.InfoWindowFragment.onAttach(InfoWindowFragment.java:175)
答案 0 :(得分:0)
我通过更改访问mapfragment的方式解决了这个问题:
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof MainActivity) {
MainActivity mainActivity = (MainActivity) context;
MapFragment parentFragment = mainActivity.getMapFragment();
if (parentFragment != null) {
mParentListener = (OnChildFragmentInteractionListener) parentFragment;
} else {
throw new RuntimeException("The parent fragment must implement OnChildFragmentInteractionListener");
}
}
}