机器人:ViewPager和HorizontalScrollVIew [英] android: ViewPager and HorizontalScrollVIew
本文介绍了机器人:ViewPager和HorizontalScrollVIew的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 HorizontalScrollView
在我的 ViewPager
。我设置
requestDisallowInterceptTouchEvent(真);
为 HorizontalScrollView
但 ViewPager
有时仍拦截触摸事件。难道还有其他的命令,我可以使用prevent视图的父母和祖先从截获的触摸事件?
I have a HorizontalScrollView
inside my ViewPager
. I set
requestDisallowInterceptTouchEvent(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?
注: HorizontalScrollView
只占据半个屏幕
推荐答案
我有同样的问题。我的解决办法是:
I had the same problem. My solution was:
- 请
ViewPager
的子类,并添加一个名为属性childId
。 - 创建一个设置为
childId
属性和设置HorizontalScrollView
。 的id
- 覆盖
onInterceptTouchEvent()
在ViewPager
的子类,如果childId
属性大于0拿到孩子,如果事件是在HorizontalScrollView
区域返回false。
- Make a subclass of
ViewPager
and add a property calledchildId
. - Create a setter for the
childId
property and set the id of theHorizontalScrollView
. - Override
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
public class CustomViewPager extends ViewPager {
private int childId;
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (childId > 0) {
View scroll = findViewById(childId);
if (scroll != null) {
Rect rect = new Rect();
scroll.getHitRect(rect);
if (rect.contains((int) event.getX(), (int) event.getY())) {
return false;
}
}
}
return super.onInterceptTouchEvent(event);
}
public void setChildId(int id) {
this.childId = id;
}
}
在的onCreate()
方法
viewPager.setChildId(R.id.horizontalScrollViewId);
adapter = new ViewPagerAdapter(this);
viewPager.setAdapter(adapter);
希望这有助于
Hope this help
这篇关于机器人:ViewPager和HorizontalScrollVIew的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文