ViewPager with Google Maps API v2: mysterious black view
Solution 1:
I was able to stop the black surface being left behind after transition by placing another view with a transparent background on top of the ViewPager
inside a FrameLayout
:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
<!-- hack to fix ugly black artefact with maps v2 -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent" />
</FrameLayout>
Solution 2:
Had the same problem with SlidingMenu and ViewPager. I noticed that control buttons in map fragment are not black in the left behind artifact.
I solved the problem by overriding the onCreateView()
method of MapFragment (SupportMapFragment)
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup view,
Bundle savedInstance) {
View layout = super.onCreateView(inflater, view, savedInstance);
FrameLayout frameLayout = new FrameLayout(getActivity());
frameLayout.setBackgroundColor(
getResources().getColor(android.R.color.transparent));
((ViewGroup) layout).addView(frameLayout,
new ViewGroup.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT
)
);
return layout;
}
Solution 3:
Google released a fix for this, but only for 4.1+ (You don't need to download a new version of PlayServices, they used a server side flag)
Here's what I'm using to bypass the issue - Ensures you don't waste any CPU cycles on devices >= 4.1
public class FixMapFragment extends SupportMapFragment {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
// Fix for black background on devices < 4.1
if (android.os.Build.VERSION.SDK_INT <
android.os.Build.VERSION_CODES.JELLY_BEAN) {
setMapTransparent((ViewGroup) view);
}
return view;
}
private void setMapTransparent(ViewGroup group) {
int childCount = group.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = group.getChildAt(i);
if (child instanceof ViewGroup) {
setMapTransparent((ViewGroup) child);
} else if (child instanceof SurfaceView) {
child.setBackgroundColor(0x00000000);
}
}
}
}