android: ViewPager 和 HorizontalScrollVIew [英] android: ViewPager and HorizontalScrollVIew
本文介绍了android: ViewPager 和 HorizontalScrollVIew的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的 ViewPager
中有一个 HorizontalScrollView
.我设置requestDisallowInterceptTouchEvent(true);
用于 HorizontalScrollView
但 ViewPager
有时仍会拦截触摸事件.是否有另一个命令可以用来防止 View 的父级和祖先拦截触摸事件?
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
只占屏幕的一半.
note: the HorizontalScrollView
only occupies half the screen.
推荐答案
我遇到了同样的问题.我的解决方案是:
I had the same problem. My solution was:
- 创建
ViewPager
的子类并添加一个名为childId
的属性. - 为
childId
属性创建一个setter 并设置HorizontalScrollView
的id. - 覆盖
ViewPager
的子类中的onInterceptTouchEvent()
,如果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.
代码
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);
希望对您有所帮助
这篇关于android: ViewPager 和 HorizontalScrollVIew的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文