android: ViewPager 和 Horizo​​ntalScrollVIew [英] android: ViewPager and HorizontalScrollVIew

查看:32
本文介绍了android: ViewPager 和 Horizo​​ntalScrollVIew的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 ViewPager 中有一个 Horizo​​ntalScrollView.我设置requestDisallowInterceptTouchEvent(true); 用于 Horizo​​ntalScrollViewViewPager 有时仍会拦截触摸事件.是否有另一个命令可以用来防止 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?

注意:Horizo​​ntalScrollView 只占屏幕的一半.

note: the HorizontalScrollView only occupies half the screen.

推荐答案

我遇到了同样的问题.我的解决方案是:

I had the same problem. My solution was:

  1. 创建ViewPager 的子类并添加一个名为childId 的属性.
  2. childId 属性创建一个setter 并设置Horizo​​ntalScrollView 的id.
  3. 覆盖 ViewPager 的子类中的 onInterceptTouchEvent(),如果 childId 属性大于 0,则获取该子类,如果事件是在 Horizo​​ntalScrollView 区域返回 false.
  1. Make a subclass of ViewPager and add a property called childId.
  2. Create a setter for the childId property and set the id of the HorizontalScrollView.
  3. 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.

代码

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 和 Horizo​​ntalScrollVIew的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆