嵌套在nestedScrollView里面的MapView

时间:2018-01-31 10:06:32

标签: java android google-maps scrollview android-nestedscrollview

我有一个应该显示地图的活动,所以我创建了一个包含mapView的单独片段,并在片段中执行必要的逻辑。

在活动中我创建了一个容器,它位于nestedScrollView内并显示地图我只是在那里提交片段并且工作正常,但是在滚动活动时有一个问题,虽然我禁用了地图的所有手势控制,但地图似乎正在移动!?

Video of the problem

活动(Java)

     void InitMap(Fragment fragment) {
    setFragment(MapFragment.newInstance(lat, lng));
  }

  void setFragment(Fragment fragment) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction =
        fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.map_container, fragment);
    fragmentTransaction.commit();
  }

活动(XML)

<android.support.design.widget.CoordinatorLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:descendantFocusability="blocksDescendants"
      android:orientation="vertical">


      <!-- Some other Views -->

      <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >


        <!-- Some other TextViews -->

        <FrameLayout
          android:layout_width="match_parent"
          android:layout_height="160dp">

          <FrameLayout
            android:id="@+id/map_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

          <View
            android:id="@+id/overlay"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"/>

        </FrameLayout>


      </LinearLayout>


    </LinearLayout>

  </android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

地图片段

public class MapFragment extends Fragment {


  private GoogleMap googleMap;
  private MapView mapView;
  private boolean mapsSupported = true;
  String lat;
  String lang;

  public static MapFragment newInstance(String lat, String lang) {
    MapFragment myFragment = new MapFragment();
    myFragment.setLat(lat);
    myFragment.setLang(lang);
    return myFragment;
  }

  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    super.onCreate(savedInstanceState);
    MapsInitializer.initialize(getActivity());

    if (mapView != null) {
      try {
        mapView.onCreate(savedInstanceState);

      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    initializeMap();
  }

  private void initializeMap() {
    if (googleMap == null && mapsSupported) {
      mapView =  getActivity().findViewById(R.id.map);
      mapView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(GoogleMap map) {
          googleMap = map;
          map.getUiSettings().setAllGesturesEnabled(false);
          map.getUiSettings().setZoomGesturesEnabled(false);
          map.getUiSettings().setScrollGesturesEnabled(false);
          map.getUiSettings().setTiltGesturesEnabled(false);
          map.getUiSettings().setRotateGesturesEnabled(false);

          Log.i( "onMapReady: ", getLat() + " " + getLang());
          double lat = Double.parseDouble(getLat());
          double lang = Double.parseDouble(getLang());
          LatLng loc = new LatLng(lat, lang);
          map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
          googleMap.addMarker(new MarkerOptions().position(loc)
              .title("Location"));
          CameraPosition cameraPosition = new CameraPosition.Builder()
              .target(loc)      // Sets the center of the map to Mountain View
              .zoom(17)                   // Sets the zoom
              .bearing(90)                // Sets the orientation of the camera to east
              .tilt(0)                   // Sets the tilt of the camera to 30 degrees
              .build();
          map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

        }
      });
    }
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View parent = (FrameLayout) inflater.inflate(R.layout.fragment_map, container, false);
    mapView = (MapView) parent.findViewById(R.id.map);
    return parent;
  }

  @Override
  public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    mapView.onSaveInstanceState(outState);
  }

  @Override
  public void onResume() {
    super.onResume();
    mapView.onResume();
    initializeMap();
  }

  @Override
  public void onPause() {
    super.onPause();
    mapView.onPause();
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    mapView.onDestroy();
  }

  @Override
  public void onLowMemory() {
    super.onLowMemory();
    mapView.onLowMemory();
  }

  public MapFragment() {
    // Required empty public constructor
  }

  public String getLat() {
    return lat;
  }

  public void setLat(String lat) {
    this.lat = lat;
  }

  public String getLang() {
    return lang;
  }

  public void setLang(String lang) {
    this.lang = lang;
  }

}

0 个答案:

没有答案