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

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

问题描述

我有一个 Horizo​​ntalScrollView 在我的 ViewPager 。我设置 requestDisallowInterceptTouchEvent(真); Horizo​​ntalScrollView 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?

注: Horizo​​ntalScrollView 只占据半个屏幕

推荐答案

我有同样的问题。我的解决办法是:

I had the same problem. My solution was:

  1. ViewPager 的子类,并添加一个名为属性 childId
  2. 创建一个设置为 childId 属性和设置 Horizo​​ntalScrollView
  3. 的id
  4. 覆盖 onInterceptTouchEvent() ViewPager 的子类,如果 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.

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

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