I had the same problem. My solution was:
- Make a subclass of
ViewPager
and add a property called childId
.
- Create a setter for the
childId
property and set the id of the HorizontalScrollView
.
- Override
onInterceptTouchEvent()
in the subclass of ViewPager
and if the childId
property is more than 0 get that child and if the event is in HorizontalScrollView
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;
}
}
In onCreate()
method
viewPager.setChildId(R.id.horizontalScrollViewId);
adapter = new ViewPagerAdapter(this);
viewPager.setAdapter(adapter);
Hope this help
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…