android - combine horizontal viewpager with vertical viewpager

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-02 03:35:07

问题


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.


回答1:


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.




回答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.




回答3:


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



来源:https://stackoverflow.com/questions/27468986/android-combine-horizontal-viewpager-with-vertical-viewpager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!