Android-禁用将滚动视图作为子级的水平滚动视图的水平滚动
我现在正在工作,以禁用水平滚动视图(id:ordermenu)的水平滚动,该水平滚动视图具有滚动视图(我的意思是垂直滚动视图。滚动视图的子相对布局的id:category01layout,category02layout,...)作为子项。
XML 中的内容如下:
HorizontalScrollView(ordermenu)
- 线性布局
- 滚动视图
-RelativeLayout(category01layout)
- 滚动视图
-RelativeLayout(category02layout)
...
- 滚动视图
-RelativeLayout(category08layout)
屏幕一次显示一个类别布局,当我按下上一个/下一个按钮时,它设置horizontalscrollview.smoothScrollTo()来更改类别布局。
因此,当我运行应用程序并滚动时,我的手指同时滚动 HorizontalScrollView 和 ScrollView。但我不想让我的手指让这两个都听 onScrollChanged。我只希望 ScrollView 听我的手指“仅垂直滚动”。因为我不知道如何做到这一点,所以只有当我沿精确的垂直方向滚动时,ScrollView 才会以平滑的方式垂直滚动。否则我的手指也会滚动 HorizontalScrollView,这是我没想到的。
那么有没有办法禁用水平滚动而只启用垂直滚动呢?
我以前使用过这种方法(创建一个扩展 HorizontalScrollView 的类),
@Override
protected void onScrollChanged(int x, int y, intoldx, intoldy){
if(Math.abs(x - oldx) <= 50){
mScrollable = false;
}
else {
mScrollable = false;
}
return;
}
但这只是给 UI 带来了很大的负担,使得垂直滚动的 ScrollView 过于僵硬和缓慢。所以请给我除此之外的另一种方法。
提前致谢。
I'm working right now in order to disable horizontal scrolling of horizontalscrollview (id: ordermenu) which has scrollview(i mean vertical scrollview. id of scrollview's child relativelayout: category01layout, category02layout, ...) as children.
Here is what it looks like in XML:
HorizontalScrollView(ordermenu)
- LinearLayout
- ScrollView
- RelativeLayout(category01layout)
- ScrollView
- RelativeLayout(category02layout)
...
- ScrollView
- RelativeLayout(category08layout)
The screen shows one category layout at a time, and when I press prev/next button, it sets horizontalscrollview.smoothScrollTo() to change the category layout.
So when I run the app and scroll, my finger is scrolling both HorizontalScrollView and ScrollView. But I don't want my finger to make both these two listen the onScrollChanged. I only want ScrollView to listen to my finger to 'only scroll vertically'. Since I don't know the way to do this, ScrollView scrolls vertically in a smooth way only if I scroll in an exact vertical direction. Otherwise my finger scrolls HorizontalScrollView also, what I didn't expect.
So is there any way to disable horizontal scrolling and only enable vertical scrolling?
I previously used this method (create a class which extends HorizontalScrollView)
@Override
protected void onScrollChanged(int x, int y, intoldx, intoldy){
if(Math.abs(x - oldx) <= 50){
mScrollable = false;
}
else {
mScrollable = false;
}
return;
}
But this just caused a lot of burden for the UI, making vertically scrolling ScrollView too stiff and slow. So please give me another way than this.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我的 HorizontalScrollView 子类中有以下方法,它允许我垂直和水平滚动。基本上,如果它检测到垂直滚动,它就会放弃触摸。根据您感觉的响应程度来选择最短距离。
I have the following method in my subclass of HorizontalScrollView that allows me to scroll both vertically and horizontally. Basically if it detects vertical scrolling it gives up the touches. Play with the min distance depending on how responsive it feels to you.
我认为你真正想要的是 ViewPager< /a>.顺便说一句,Google 的 Android 开发人员经常提到以下规则:
I think what you really want here is a ViewPager. Incidentally, Google's Android devs mention the following rules often:
除了您的这个问题之外,我建议您使用 View Flipper。
然后在其中获取布局(单击下一个/上一个按钮后将更改的布局)。
现在,当单击下一个按钮时,请执行带有动画的 viewFlipper.showNext()/viewFlipper.showPrevious() (如右进/左出)。
因此,您可以在布局上显示平滑的滚动。尝试一下。我就是这样做的。它应该有效。
Apart from your this problem i suggest you to take a View Flipper.
Then take the layouts(those layouts which will be changed after clicking the next/prev button) in it.
Now when the next button will be clicked do viewFlipper.showNext()/viewFlipper.showPrevious() with animation(like right in/left out).
Thus you can show a smooth scrall over layouts. Try it. I do it in this way. It should work.
尝试使用视图切换器,其中不需要在其他滚动视图内滚动视图和在滚动视图内滚动视图。
我希望你尽快解决你的问题。
Try to use view switcher,In which there is no need to scroll views inside other scroll views and list views inside scroll views.
I wish you solve your problem very soon.