Android MapView 使用 ViewPager 和 PageTransformer 消失

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

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

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

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

XML:ViewPager fragment

<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>

map fragment :

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这给我指明了正确的方向。 Google Play 服务中似乎存在一些错误,可以通过将 map 设置为“精简模式”来解决。在我的例子中,它成功了, map 现在可以在 pagetransformer 中工作了。据我了解,精简模式将 map 更改为位图图像。您可以在这里阅读更多相关信息 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);

关于Android MapView 使用 ViewPager 和 PageTransformer 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29985454/

相关文章:

java - 从 NavaigationDrawer 内的共享首选项获取电子邮件

android - android 中用于在社交媒体中共享的公共(public)库?

java - 谷歌 adt/gwt 支持 eclipse 4.4 (Luna)

android - fragment Activity : Cannot cast from Fragment to derived class

android - 带有 fragment 的布局设置

android - Viewpager 变得太高

android - 快速滑动时奇怪的 View 寻呼机行为

android - 想要完全删除 ListView 适配器项?

Android - 不断更新 fragment 用户界面?

android - 在包含 ViewPager 的 ListView 上实现 ViewHolder 模式