MapFragment causes flickering in ViewPager

2019-04-16 06:35发布

问题:

I use a (Support)MapFragment inside a (Support)ViewPager, filled by a FragmentPagerAdapter.

The ViewPager consists of three fragments, with the map fragment on the right: |x|x|map|. When I swipe to the right from the first fragment, die whole UI visibly redraws as the MapFragment is loaded and the screen flickers. Once the map is loaded for the first time swiping between screens works fine.

How can I fix this?

回答1:

I had the same issue with my project's (rather complex) structure: HostActivity -> HostFragment with ViewPager -> ListView with embedded MapFragment.

I tried to solve this issue following this gmaps-api-issues thread but with no luck (though I recommend reading it as it provides some background on the issue itself).

Then I stumbled upon this blog post and it did the trick for me:

public void onCreate(Bundle savedInstanceState) {
    ...
    ViewPager pager = (ViewPager)findViewById(R.id.pager);
    ...
    pager.setAdapter(adapter);
    pager.requestTransparentRegion(pager); // This line solved the problem for me
    ...
}