是否可以在bottomNavigation中使用活动项和片段?

时间:2019-02-14 14:55:55

标签: android android-layout android-fragments android-activity

Screenshot of the app

我正在尝试在应用程序的选项卡上实现MapView,但是在将其实现为片段而不是活动时遇到了问题。我设法显示了用户的位置,但是由于似乎没有调用onLocationChanged方法而出现问题。将其作为一项活动实施为选项卡中的一个选项卡,并为其他选项卡使用片段会更容易吗?

public class LocationFragment extends Fragment implements
    OnMapReadyCallback,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    LocationListener {


private GoogleMap mGoogleMap;
private MapView mMapView;
private View mView;
private GoogleApiClient apiClient;
private LocationRequest locationRequest;
private LocationManager locationManager;
private Location lastKnowLocation;
private FragmentActivity myContext;
private Marker currentLocationMarker;
final int userRequestCode = 1;

public LocationFragment() {

}

@Overide
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
        checkUserLocationPermission();
    }

}

 @Overide
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mView = inflater.inflate(R.layout.fragment_location, container, false);
    return mView;
}

 @Overide
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    mMapView = mView.findViewById(R.id.map);
    if (mMapView != null) {
        mMapView.onCreate(null);
        mMapView.onResume();
        mMapView.getMapAsync(this);
    }
}

 @Overide
public void onLocationChanged(Location location) {

    lastKnowLocation = location;
    if (currentLocationMarker != null) {
        currentLocationMarker.remove();
    }


    LatLng currentLatLng = new LatLng(location.getLatitude(), location.getLongitude());

    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(currentLatLng);
    markerOptions.title("Current Location");
    markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
    currentLocationMarker = mGoogleMap.addMarker(markerOptions);

    mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(currentLatLng));
    mGoogleMap.animateCamera(CameraUpdateFactory.zoomBy(16));

    locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);

    if (apiClient != null) {

        if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, (android.location.LocationListener) LocationFragment.this);
        }
    }

}


 @Overide
public void onConnected(@Nullable Bundle bundle) {

    locationRequest = new LocationRequest();
    locationRequest.setInterval(510);
    locationRequest.setFastestInterval(510);
    locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

    locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);

    if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0, (android.location.LocationListener) LocationFragment.this);

    }
}

1 个答案:

答案 0 :(得分:1)

您需要具有一个一个活动,其中包括一个框架布局,将其用作显示5个不同的片段的阶段。如果您不明白,请告诉我一些代码。

更新

public class MapFragment extends Fragment {

}

public static MapFragment newInstance(int i) {
    MapFragment f = new MapFragment();
    return f;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.map_fragment, container, false);

    return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

}