Android MapView 使用 ViewPager 和 PageTransformer 消失

Android MapView disappears using a ViewPager and PageTransformer

我在使用 PageTransformer 和 MapView 设置 ViewPager 时遇到问题。问题是当 viewpager 设置为使用 PageTransformer 时,mapview 在平移时消失(变为透明)。但是,如果我不使用转换器而只是平移 Mapview 就可以了。有谁知道是什么导致了这种行为?

目前 PageTransformer 没有做任何特别的事情,只是设置它似乎会影响 mapview 的绘制方式。我应该注意到地图变得透明,而地图视图(法律声明)仍然存在。当 ViewPager 进入空闲状态时,地图出现。

viewPager.setPageTransformer(true, this);
...
@Override
public void transformPage(View view, float position) {
}

XML:ViewPager 片段

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF">
        <com.google.android.gms.maps.MapView
                    android:id="@+id/mapcontainer"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:clickable="false"/>
</FrameLayout>

地图片段:

private GoogleMap map;
private MapView mapView;
private Bundle mapBundle;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    mapBundle = savedInstanceState;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.mapfragment, container, false);
    MapsInitializer.initialize(getActivity());
    mapView = (MapView) rootView.findViewById(R.id.mapcontainer);
    mapView.onCreate(mapBundle);

    map = mapView.getMap();

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

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

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

我从讨论帖 Bug: Black rectangle background on scroll that pointed me in the right direction. There seems to be some bugs in Google Play Services that can be resolved by setting the map to "Lite Mode". In my case it did the trick and the map is now working in the pagetransformer. From what I understand lite mode changes the map to a Bitmap Image. You can read more about it here Google Maps - Lite Mode 中找到了解决方案。精简模式有一些限制,例如无法平移或缩放,但在我的情况下,这不是必需的。

我将 XML 更新为:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
    <com.google.android.gms.maps.MapView
                xmlns:map="http://schemas.android.com/apk/res-auto"
                android:id="@+id/mapcontainer"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:clickable="false"
                map:liteMode="true"/>
</FrameLayout>

您也可以在代码中设置此选项:

GoogleMapOptions options = new GoogleMapOptions().liteMode(true);
MapView mapView = MapView (context, options);