如何在选项卡的片段中实现 MapFragment
How to implement a MapFragment in an Fragment of a Tab
我有几个使用 FragmentPagerAdapter
创建的选项卡,在这个适配器中,我使用 getItem
方法获得我的 "Tab-Fragments":
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new MapsFragment();
case 1:
return new GroupsFragment();
case 2:
return new InfosFragment();
}
return null;
}
现在我想在我的 1.Tab 上实现一个 MapFragment 我已经有了这个:
public class MapsFragment extends Fragment implements OnMapReadyCallback {
private MapFragment mapFragment;
public static MapsFragment newInstance() {
MapsFragment fragment = new MapsFragment();
return fragment;
}
public MapsFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_maps, container, false);
}
@Override
public void onMapReady(GoogleMap googleMap) {
LatLng augsburg = new LatLng(48.348527, 10.915952);
googleMap.setMyLocationEnabled(true);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(augsburg, 13));
googleMap.addMarker(new MarkerOptions()
.title("Augsburg Zoo")
.snippet("Der coolste Zoo der Welt")
.position(augsburg));
}
}
但我在这一行收到类型不兼容的错误:
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
但是我不知道如何修复它以及如何编写比这更好的代码!
您可能正在使用 SupportMapFragment。
将 onCreate() 更改为
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_maps, container, false);
SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
return view;
}
我有几个使用 FragmentPagerAdapter
创建的选项卡,在这个适配器中,我使用 getItem
方法获得我的 "Tab-Fragments":
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new MapsFragment();
case 1:
return new GroupsFragment();
case 2:
return new InfosFragment();
}
return null;
}
现在我想在我的 1.Tab 上实现一个 MapFragment 我已经有了这个:
public class MapsFragment extends Fragment implements OnMapReadyCallback {
private MapFragment mapFragment;
public static MapsFragment newInstance() {
MapsFragment fragment = new MapsFragment();
return fragment;
}
public MapsFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_maps, container, false);
}
@Override
public void onMapReady(GoogleMap googleMap) {
LatLng augsburg = new LatLng(48.348527, 10.915952);
googleMap.setMyLocationEnabled(true);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(augsburg, 13));
googleMap.addMarker(new MarkerOptions()
.title("Augsburg Zoo")
.snippet("Der coolste Zoo der Welt")
.position(augsburg));
}
}
但我在这一行收到类型不兼容的错误:
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
但是我不知道如何修复它以及如何编写比这更好的代码!
您可能正在使用 SupportMapFragment。
将 onCreate() 更改为
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_maps, container, false);
SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
return view;
}