【问题标题】:Android MapView disappears using a ViewPager and PageTransformerAndroid MapView 使用 ViewPager 和 PageTransformer 消失
【发布时间】:2015-05-01 10:40:48
【问题描述】:

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

目前 PageTransformer 没有什么特别的,只是设置它似乎会影响地图视图的绘制方式。我应该注意到地图是透明的,而地图视图(法律声明)仍然存在。当 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();
}

【问题讨论】:

  • 对于 MapView,使用处理程序插入一些后期延迟以在您的 viewpager 中加载地图。告诉我它解决了你的问题

标签: android android-fragments android-viewpager android-mapview android-pagetransformer


【解决方案1】:

我从讨论帖Bug: Black rectangle background on scroll 中找到了一个解决方案,它为我指明了正确的方向。 Google Play 服务中似乎有一些错误可以通过将地图设置为“精简模式”来解决。就我而言,它成功了,地图现在在 pagetransformer 中工作。据我了解,精简模式将地图更改为位图图像。你可以在这里阅读更多关于它的信息Google Maps - Lite Mode。 Lite 模式有一些限制,例如无法平移或缩放,但在我的情况下,这不是必需的。

我将我的 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);

【讨论】:

    猜你喜欢
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多