6

I want to combine a default horizontal ViewPager with some kind of a vertical ViewPager. My approach would be that the Fragments provided by the horizontal ViewPager, are subclassing the vertical ViewPager.

public class SubWebViewFragment extends Fragment, VerticalViewPager {
}

Thus each Fragment provided by the horizontal ViewPager should at the same time act as a VerticalViewPager, building up some kind of a matrix. In addition I have to have the possibility to go to a certain page in this matrix. E.g. I want to select page 2 of the horizontal ViewPager, and of this page two I want to go to page three of the vertical ViewPager.

// pseudo code
HorizontalViewPager.setCurrentItem(1, true);
activeHorizontalPage.getVerticalViewPager.setCurrentItem(2, true);

I'm a bit lost on how to approach this issue.

Community
  • 1
  • 1
Thomas Kremmel
  • 13,615
  • 21
  • 104
  • 169

3 Answers3

12

I built a solution combining a horizontal viewpager (the parent) with vertical viewpagers (each child). I overrode the following methods on vertical viewpager:

  • public boolean onInterceptTouchEvent(MotionEvent ev)
  • public boolean onTouchEvent(MotionEvent ev)

When the user triggers those events on each child, they pass it to the parent. Then, if the event is vertical, the child processes it, otherwise, if the event is horizontal, the parent processes it.

Take a look to my DoubleViewPager library, where I implemented this strategy.

Julio Gómez
  • 310
  • 7
  • 17
  • It is not working with FragmentPagerAdapter or FragmentStatePagerAdapter. – ycagri Nov 06 '15 at 10:09
  • If DoubleViewPagerAdapter extends from FragmentPagerAdapater ot FragmentStatePagerAdapter, it is not working properly. For the first column, vertical swipe works but it does not populate other columns. – ycagri Nov 09 '15 at 08:03
  • @ycagri ok, i will try to fix it. – Julio Gómez Nov 12 '15 at 11:34
  • @JulioGómez It seems that you have to use same adapters in both pagers. To be more precise, both adapters have to be either FragmentStatePagerAdapter or PagerAdapter. If you use PagerAdapter on horizontal pager and FragmentStatePagerAdapter on vertical pager, item population does not work correctly. I thought this info might be useful for your code. – ycagri Nov 19 '15 at 12:47
  • Julio, the library was very helpful to me. Can you please tell how do I add page change listener to vertical view? I want to detect which the current horizontal and vertical page now? – Sam Dec 11 '16 at 15:19
  • Hi @SameerThigale, right now it's not supported by the library, but i will work on it! – Julio Gómez Dec 12 '16 at 07:49
  • Hi @JulioGómez, I want to load images into the ViewPager. However, I get out of memory exception after a few images. Can you please let me know how can I dynamically load images only when view is visible to user? – Sam Dec 13 '16 at 13:13
2

This DoubleViewPager project on GitHub seems to have implemented a simple version of what you're asking for. He has a customized HorizontalViewPager and VerticalViewPager derived from the ViewPager class that allows for the page stack structure you're asking for.

hnzly
  • 21
  • 5
-1

Have you thought of using a listview instead of a vertical viewpager also try checking out theTwowayview library

christoandrew
  • 397
  • 2
  • 15
  • I think ListView is not really possible. See: http://stackoverflow.com/a/8457633/237690 .. Maybe Twowayview is a possibility. Thank you.. – Thomas Kremmel Dec 14 '14 at 15:34