Я использую макет вкладки в моем проекте.На одном из фрагментов вкладки должна отображаться карта мира.Мое приложение интегрировано с Google map API, и я использовал ViewPager для настройки адаптера.Ниже приведена деятельность.
public class HomeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
PagerAdapter pagerAdapter =
new PagerAdapter(getSupportFragmentManager(), HomeActivity.this);
viewPager.setAdapter(pagerAdapter);
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(viewPager);
// Iterate over all tabs and set the custom view
for (int i = 0; i < tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
tab.setCustomView(pagerAdapter.getTabView(i));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
class PagerAdapter extends FragmentPagerAdapter {
String tabTitles[] = new String[] { "Cloud", "Home", "Report" };
Context context;
public PagerAdapter(FragmentManager fm, HomeActivity context) {
super(fm);
this.context = context;
}
@Override
public int getCount() {
return tabTitles.length;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new MapFragment();
case 1:
return new OneFragment();
case 2:
return new TwoFragment();
}
return null;
}
@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
public View getTabView(int position) {
View tab = LayoutInflater.from(HomeActivity.this).inflate(R.layout.custom_tab, null);
TextView tv = (TextView) tab.findViewById(R.id.custom_text);
tv.setText(tabTitles[position]);
return tab;
}
}
}
И мой фрагмент
public class MapFragment extends Fragment {
private MapView mMapView;
private GoogleMap mMap;
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_map, null, false);
mMapView = (MapView) view.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
Toast.makeText(
getActivity(),
"Lat : " + latLng.latitude + " , "
+ "Long : " + latLng.longitude,
Toast.LENGTH_SHORT).show();
}
});
if (mMap == null) {
mMapView = (MapView) view.findViewById(R.id.mapView);
mMapView.onCreate(getArguments());
mMapView.onResume();
try {
MapsInitializer.initialize(getActivity());
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap gmap) {
mMap = gmap;
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
//fill markers
//add marker listener
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
return view;
}
@Override
public void onResume(){
super.onResume();
mMapView.onResume();
}
@Override
public void onPause(){
super.onPause();
mMapView.onPause();
}
@Override
public void onDestroy(){
super.onDestroy();
mMapView.onDestroy();
}
@Override
public void onLowMemory(){
super.onLowMemory();
mMapView.onLowMemory();
}
}
мое приложение успешно работает.и на первой вкладке фрагмент может быть загружен и может видеть значения широты и долготы также при нажатии на экран.но не в состоянии увидеть карту Google.Размещено под изображением.один и тот же код отлично работает в отдельной деятельности.Пожалуйста, помогите найти то, что я пропустил, чтобы добавить. Скриншот