3 回答

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超13個(gè)贊
謝謝,發(fā)現(xiàn)這很有幫助。我正在發(fā)布我稍作修改的解決方案,因?yàn)楫?dāng)?shù)貓D準(zhǔn)備好時(shí),我可以更清楚地告訴父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 關(guān)注
- 493 瀏覽
添加回答
舉報(bào)