2017-07-15 4 views
-1

私はタブアクティビティを持っており、最初のタブではマップフラグメントを表示します。 私の問題は、私はそれをする方法を知らないということです。 私はsupportMapFragmentを拡張するマップフラグメントを持っていますが、私はどのように私がタブでそのフラグメントを表示するか分かりません。Android - タブアクションでmapFragmentを表示する方法

、これが主な活動]タブ

public class SectionsPagerAdapter extends FragmentPagerAdapter { 

    public SectionsPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     // getItem is called to instantiate the fragment for the given page. 
     // Return a PlaceholderFragment (defined as a static inner class below). 
     switch (position) { 
      case 0: 
       // here i want to show the mapFragment. how should i do that? 
       return new Maps(); 
      case 1: 
       return new GroupsFragment(); 
      default: 
       return new Fragment(); 
     } 
    } 

EDIT MapActivity

public class MapsActivity extends SupportMapFragment implements OnMapReadyCallback { 

private static final int RC_LOCATION = 1; 
private GoogleMap mMap; 

@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 
    getMapAsync(this); 
} 



@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 

    addMyLocation(); 

} 

private boolean checkLocationPermission(){ 
    String[] permissions = new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}; 
    //If No Permission-> Request the permission and return false. 
    if (ActivityCompat.checkSelfPermission(getContext(), 
      android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

     ActivityCompat.requestPermissions(getActivity(), permissions, RC_LOCATION); 
     return false; 
    } 
    return true;//return true if we have a permission 
} 

private void addMyLocation(){ 
    if (!checkLocationPermission())return; 
    mMap.setMyLocationEnabled(true); 
    mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() { 
     @Override 
     public boolean onMyLocationButtonClick() { 
      if (mMap.getMyLocation()!=null) { 
       Location myLocation = mMap.getMyLocation(); 
       Toast.makeText(getActivity(), "" + myLocation.getLatitude(), Toast.LENGTH_SHORT).show(); 
      } 
      return false; 
     } 
    }); 
} 
@Override 
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    if (grantResults[0] == PackageManager.PERMISSION_GRANTED){ 
     //noinspection MissingPermission 
     addMyLocation(); 
    } 
} 

}

+0

地図を投稿する – FAT

+0

mapActivityを投稿しました –

答えて

0

この

を試してみてくださいです
public class SectionsPagerAdapter extends FragmentPagerAdapter { 

    public SectionsPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     // getItem is called to instantiate the fragment for the given page. 
     // Return a PlaceholderFragment (defined as a static inner class below). 
     switch (position) { 
      case 0: 
       SupportMapFragment mapFragment = SupportMapFragment.newInstance(); 
       mapFragment.getMapAsync(new OnMapReadyCallback() { 
        @Override 
        public void onMapReady(GoogleMap googleMap) { 

        } 
       }); 

       return mapFragment; 
      case 1: 
       return new GroupsFragment(); 
      default: 
       return new Fragment(); 
     } 
    } 

    @Override 
    public int getCount() { 
     return 3; 
    } 
} 
関連する問題