EDIT: See my own answer for easy solution
IMPORTANT: Bounty is offered for clear way to modify ViewPager to satisfy the scenario outlined below. Please do not offer HorizontalScrollView - I need full Fragment lifecycle scenario covered
I need to implement horizontal scrolling of Fragments
-based views in which one item is in the center and items to the right/left are partially or fully visible. ViewPager
is ill suitable for the task since it's focused on displaying one item at each time.
To make it easier to understand below is a quick sketch in which items 1, 5 and 6 are outside of viewable area. And and want to make this viewable number configurable so for example in portrait view I will only show 2 (or possibly just one) items.
I'm not trying to fit say 3 items on the screen, as long as central item is shown others can be cropped. On the small screen is OK to have 1 central item and as screen grows in size multiple (cropped is OK) items should be shown
I understand that this looks like a gallery but again the items are not simple images but Fragments
with a vertically scrollable list in each fragment
P.S. Found this blogpost by @Commonsware that list 3 different approaches. For my need I like #3
This one has surprisingly easy answer, I'm not even sure why it wasn't posted right away. All that I needed to do to get the exact effect was to override PagerAdapter#getPageWidth
method. By default it returns 1 but if you set it to 0.5 you will get 2 pages, 0.33 will give you 3, etc. Depending on width of the separator between pager items you may have to slightly decrease the value.
See the following snippet:
@Override
public float getPageWidth(final int position) {
// this will have 3 pages in a single view
return 0.32f;
}