Drag an image only horizontally or vertically in android?

好久不见. 提交于 2019-11-29 18:29:24

First, put a ScrollView inside a HorizontalScrollView, and the ImageView inside the innermost ScrollView.

Hierarchy will look like this:

  LinearLayout (ROOT)
  |
  +-- ScrollView (V)
       |
       +-- LinearLayout (1)
            |
            +-- HorizontalScrollView (H)
                |
                +-- LinearLayout (2)
                    |
                    +-- ImageView

And let me explain:

(V) scrolls in vertical direction only.

(H) scrolls in horizontal direction only.

(1) and (2) are linear layouts used immediately following any ScrollViews. Because, a ScrollView can host only one direct child.

Note on naming: There's only "ScrollView", and not "VerticalScrollView"

What happens when you drag the ImageView ?

Based on the very first direction of the gesture you make, if it's horizontal, the HorizontalScrollView will process the action, and the ScrollView will sit idle. And if the action was vertical, the ScrollView will process the action, and HorizontalScrollView will sit idle.

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