Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - combine horizontal viewpager with vertical viewpager

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.

like image 863
Thomas Kremmel Avatar asked Dec 14 '14 11:12

Thomas Kremmel


2 Answers

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.

like image 181
Julio Gómez Avatar answered Nov 14 '22 22:11

Julio Gómez


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.

like image 25
hnzly Avatar answered Nov 14 '22 21:11

hnzly