I can't get my ViewPager
not clipping its pages contents.
I'm currently using ViewPager
with a FragmentStatePagerAdapter
in my application. I have 3 pages, each page is showing a different fragment.
Everything works fine, but now I want to be able to draw outside of my pages bounds, like in this image:
I set clipChildren
and clipToPadding
to false
in my all views parents to the views that needs to draw outside their bounds, but here's what I get : my views get clipped according to pages bounds.
Some additional info : In order to fix that issue, I used hierarchyviewer
to check my view hierarchy and see if some "under the hood" views could have a clipping behaviour. And bingo : each of my fragments' root views is added under a NoSaveStateFrameLayout
. I suspect this guy to clip its children...
Is my last assumption correct in your opinion? How would you do to get unclipped pages?
There are a few different Views that require disabled clipping. First, the FrameLayout that is at the root of the ViewPager needs to have clipping disabled. You can do this wherever you initialize your view pager. You also need to set the off-screen page limit to 2 to allow for an extra fragment to be loaded during a swipe. Without this, you'll see a pop going from 1->2 as 3 loads and adds it's overflow content to 2 once 2 has finished loading (this loads both 2 and 3 ahead of time).
Second, the XML you inflate in your FragmentStatePagerAdapter needs to include the appropriate tags or it can be done programmatically. Last, you need to force the NoSaveStateFrameLayout to not clip as well. This can be done after the view has been created in onViewCreated. Here's an example fragment:
I have tested this solution and am certain it works.
Full activity example: https://gist.github.com/grantamos/d664f01a30f9ad97ba53
Have you tried to use a negative value for the margin between pages (using
ViewPager.setPageMargin(int)
)? You will also have to set offscreen page limit to at least 2 (usingViewPager.setOffscreenPageLimit(int)
).I had the same problem. I fixed this by adding
to
ViewPager
andViewGroup
which is parent ofViewPager
For instance, if you have a layout like that:
then only
LinearLayout
andViewPager
should have