Pages

2013/07/25

Prevent flickering MapFragment on ViewPager in Android

If you add MapFragment(or SupportMapFragment) to ViewPager, there is some flickering(or blinking, the screen become black for a while) when the SupportMapFragment is initialized. See what happen to MapFragment in ViewPager :




In my case, I used SupportMapFragment for compatibility in Support ViewPager. The ViewPager consists of three fragments:

|Fragment #1| Fragment #2|Fragment Map|.

The map fragment is on the right. When I swipe to the right from the first fragment, whole screen is blinking for a while like the video. There is no blinking after loading the mapfragment first time.

This is an limitation of Android with using GLSurfaceView.
My solution is below:


// This FragmentActivity contains a ViewPager and the ViewPager has 3 fragments. 
// SupportMapFragment is one of them. 
public class GymDetailActivity extends SherlockFragmentActivity{
@Override
    public void onCreate(Bundle savedInstanceState) {
        /* ... */
        ViewPager pager = (ViewPager)findViewById(R.id.detail_main_pager);
        pager.setAdapter(adapter);
        
        pager.requestTransparentRegion(pager);  //This is the solution! 

    }
}

The problem may come again on some devices, but I think this is the best solution what ever I met before.

No comments:

Post a Comment