android: ViewPager 和 HorizontalScrollVIew
我的 ViewPager
中有一个 HorizontalScrollView
。我设定 requestDisallowInterceptTouchEvent(true);
对于 HorizontalScrollView
但 ViewPager
有时仍然会拦截触摸事件。是否还有另一个命令可以用来防止视图的父级和祖先拦截触摸事件?
注意:HorizontalScrollView
仅占据屏幕的一半。
I have a HorizontalScrollView
inside my ViewPager
. I setrequestDisallowInterceptTouchEvent(true);
for the HorizontalScrollView
but the ViewPager
is still sometimes intercepting touch events. Is there another command I can use to prevent a View's parent and ancestors from intercepting touch events?
note: the HorizontalScrollView
only occupies half the screen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我也有同样的问题。我的解决方案是:
ViewPager
的子类并添加一个名为childId
的属性。childId
属性创建一个 setter,并设置HorizontalScrollView
的 id。ViewPager
子类中的onInterceptTouchEvent()
,如果childId
属性大于 0,则获取该子级,并且如果事件位于中>HorizontalScrollView
区域返回 false。代码
onCreate()
方法中的希望有帮助
I had the same problem. My solution was:
ViewPager
and add a property calledchildId
.childId
property and set the id of theHorizontalScrollView
.onInterceptTouchEvent()
in the subclass ofViewPager
and if thechildId
property is more than 0 get that child and if the event is inHorizontalScrollView
area return false.Code
In
onCreate()
methodHope this help
感谢您的回复。我稍微修改了您的解决方案并设法使嵌套 ViewPager 工作:
Thanks for the reply. I modified your solution a bit and managed to make nested ViewPagers work:
所以,我知道这个问题已经很老了,但我提出了一个解决方案,我认为它比此处发布的解决方案具有一些额外的优势。可能发生的一种情况是,我的 ViewPager 中可能有多个 HorizontalScrollView (我就是这样做的),然后我需要向 ViewPager 提供大量 Id,以便它始终可以检查子触摸冲突。
我分析了ViewPager的源代码,发现他们使用这个名为“canScroll”的方法来确定子视图是否可以滚动。幸运的是,它不是静态的也不是最终的,所以我可以覆盖它。在该方法中,调用了“boolean ViewCompat.canScrollHorizontally(View, int)”(对于 SDK < 14 总是返回 false 的方法)。我的解决方案是:
所以,如果您的 ViewPager 中有一个不同的 View 类,它也应该接收水平拖动,只需更改 customCanScroll(View) ,这样您就可以返回是否不应拦截触摸。
如果当前视图也应该检查“可滚动性”,则布尔值 checkV 为 true,这就是为什么我们也应该检查这一点。
希望这对未来类似的问题有所帮助(或者如果有更好的解决方案,或者 Android 平台的官方解决方案,请告诉我)。
So, I know this question is pretty old, but I've come with a solution which I think has some extra advantages over the one posted here. One situation that could happen is that I could have more than one HorizontalScrollView inside my ViewPager (which I do), and then I would need to provide tons of Ids to the ViewPager so it can always check for child touch conflicts.
I analyzed the source from the ViewPager, and found that they use this method called "canScroll" to determine if a child view can be scrolled. Luckily, it wasn't made static nor final, so I could just override it. Inside that method that the "boolean ViewCompat.canScrollHorizontally(View, int)" was called (the one that always returns false for SDK < 14). My solution then was:
So, if you have a different class of View inside your ViewPager that should also receive horizontal drags, just change the customCanScroll(View) so you can return if the touch should not be intercepted or not.
The boolean checkV is true if the current view should also be checked for 'scrollability', that's why we should also check for that.
Hope this helps future issues like this (or if there is a better solution, or an official one from the Android platform, let me know).
我不知道你是否已经解决了这个问题。但上面的答案都不能正常工作。所以我认为对于那些可能正在痛苦地寻找解决方案的人来说这是必要的。
其实如果你阅读了关于在ViewGroup中处理触摸事件的官方文档,解决方案非常简单。
的用法
首先你需要了解ViewParent.requestDisallowInterceptTouchEvent(boolean) ,在父视图上调用此方法以指示它不应拦截带有 onInterceptTouchEvent(MotionEvent)。
然后,您需要拦截事件并确保 HorizontalScrollView 的父 ViewGroup 不会进一步分派事件。
因此,为 HorizontalScrollView 设置一个 OnToushListener。检测触摸和移动事件,然后将 requestDisallowInterceptTouchEvent(true) 设置为其父级(ViewPager 或其他),以确保事件将由 HorizontalScrollView 处理。
当然,您可以将下面的这些代码放入您的 CustomHorizontalScrollView 组件中,以便可以重用。
希望有帮助。 ;-)
I don't know if you've solved the problem already or not. But none of the answers above work properly. So I think it's necessary for those people who might be searching the solutions with suffering.
In fact, the solution is quite simple if you read the official document about handling touch events in ViewGroup.
First you need to understand the usage of
ViewParent.requestDisallowInterceptTouchEvent(boolean), Call this upon a parent View to indicate that it should not intercept touch events with onInterceptTouchEvent(MotionEvent).
Then you need to intercept the events and make sure that the parent ViewGroup of your HorizontalScrollView won't dispatch the events any further.
So, set an OnToushListener for you HorizontalScrollView. Detecting the touch down and move events, then set the requestDisallowInterceptTouchEvent(true) to it's parent(ViewPager or sth else) to make sure the events would be handled by the HorizontalScrollView.
Of course you could put these code below into your CustomHorizontalScrollView components so that it can be reused.
Hoping it helps. ;-)
这是另一个对我有用的解决方案,
创建一个自定义 ViewPager
然后将 OnTouchListener 添加到您的项目中,无论它是 HorizontalScrollView 或 Gallery 还是您不希望 viewpager 在触摸时滚动的任何其他内容。
Here is another solution that worked for me,
Create a custom ViewPager
And then add OnTouchListener to your item, whether it was a HorizontalScrollView or Gallery or anything else that you don't want the viewpager to scroll when it is touched.
Fede 的解决方案对我来说效果不太好。我的 HorizontalScrollVIew 有一些区域无法捕获任何输入(单击、滑动......)。我想 getHitRect() 必须对此做一些事情。用以下代码替换它后一切正常。
可以添加多个 HorizontalScrollVIew 视图或任何其他需要焦点的视图。
在 XML 中,您将看到如下内容:
在 onCreate() 方法中:
在视图的 OnTouchListener() 方法中,您已添加到 CustomViewPager 子项:
Fede's solution didn't work very well for me. My HorizontalScrollVIew had areas where it didn't catch any inputs (click, swipe..). I guess getHitRect() have to do something with that. After replacing it with following code everything worked fine.
It is possible to add multiple HorizontalScrollVIew views or any other views that requires focus too.
In XML you will have something like this:
In onCreate() method:
In OnTouchListener() method for view you have added to CustomViewPager children:
我在查看页面组中第一页底部的小画廊视图时遇到问题。滑动图库视图焦点将在移动图库项目或视图分页器之间跳转。
重写上面列出的 ViewPager 并设置 requestDisallowInterceptTouchEvent 解决了我的问题。滑动图库视图时页面不再更改。对于那些使用 galleryviews 和 viewpager 的人来说,它是一个重要的解决方案。
I was having trouble with a small gallery view on the foot of my first page in a viewpager group. On swiping the gallery view focus would jump between moving the gallery items or the viewpager.
Overriding ViewPager as listed above and setting requestDisallowInterceptTouchEvent solved my problem. The pages no longer change when swiping the galleryview. Think its an important solution for those using galleryviews with viewpager.
因此,我修改了 Fede 的答案,因为它禁用了我想要的页面周围同一区域的触摸。另外,我使它能够添加多个视图,因为我正在处理大量使用水平拖/放交互性的图形和交互式视图。这样你就可以添加
}
So, I modified Fede's answer because it was disabling the touch in the same area in the pages around the one I wanted. Also, I made it able to add multiple views because I'm dealing with a lot of graphics and interactive views that use horizontal drag/drop interactivity. This way you can add
}
经过大量测试后,我发现了这一点。
在 MyPagerView 中:
after test a lot of solution, I find this.
in MyPagerView: