3 回答
TA贡献1836条经验 获得超13个赞
谢谢,发现这很有帮助。我正在发布我稍作修改的解决方案,因为当地图准备好时,我可以更清楚地告诉父Fragment。此方法还可以与saveInstanceState / restoreInstanceState周期一起使用。
public class CustomMapFragment extends SupportMapFragment {
private static final String LOG_TAG = "CustomMapFragment";
public CustomMapFragment() {
super();
}
public static CustomMapFragment newInstance() {
CustomMapFragment fragment = new CustomMapFragment();
return fragment;
}
@Override
public View onCreateView(LayoutInflater arg0, ViewGroup arg1, Bundle arg2) {
View v = super.onCreateView(arg0, arg1, arg2);
Fragment fragment = getParentFragment();
if (fragment != null && fragment instanceof OnMapReadyListener) {
((OnMapReadyListener) fragment).onMapReady();
}
return v;
}
/**
* Listener interface to tell when the map is ready
*/
public static interface OnMapReadyListener {
void onMapReady();
}
}
用作嵌套片段:-
public class ParentFragment extends Fragment implements OnMapReadyListener {
...
mMapFragment = CustomMapFragment.newInstance();
getChildFragmentManager().beginTransaction().replace(R.id.mapContainer, mMapFragment).commit();
@Override
public void onMapReady() {
mMap = mMapFragment.getMap();
}
...
}
希望它可以帮助某人。
- 3 回答
- 0 关注
- 452 浏览
添加回答
举报