通过自定义2D滚动型父抓住onTouch事件,处理它,并传给孩子 [英] Catch onTouch event by Custom 2d Scrollview parent, handle it and pass to children

查看:156
本文介绍了通过自定义2D滚动型父抓住onTouch事件,处理它,并传给孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现的 <一个href="http://stackoverflow.com/questions/12168254/catch-ontouch-event-by-parent-handle-it-and-pass-to-children">Catch onTouch活动由家长,处理,并传递给孩子 的屁股没能做到这一点。

全二维滚动code要点是 这里

我曾尝试:

覆盖的方法 dispatchTouchEvent ,并派MotionEvent到GestureDetector

  @覆盖
     公共布尔dispatchTouchEvent(MotionEvent EV){

    //返回super.dispatchTouchEvent(EV);

     布尔结果= super.dispatchTouchEvent(EV);
     如果(gestureDetector.onTouchEvent(EV)){
            返回结果;
       }
       //如果不向垂直方向(大于x的Y),不劫持事件。
        其他 {
            返回false;
       }
 

}

活动:

 的setContentView(R.layout.activity_fullscreen);

    最后查看内容查看= findViewById(R.id.fullscreen_content);

    android.support.v4.app.FragmentManager FM = getSupportFragmentManager();
    CTX =这一点;

   ShapeDrawableView animView =新ShapeDrawableView(本,FM,CTX); // customview



    ((ViewGroup中)内容查看).addView(animView);
 

布局:

 &LT;查看
   类=com.example.astream.TwoDScrollView
   机器人:ID =@ + ID / fullscreen_content
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
     机器人:重力=中心/&GT;
 

解决方案

可以非常详细地不检查的权利,但这种凸现出一个问题:

  //如果没有向垂直方向(大于x的Y),不劫持事件。
其他 {
    返回false;
}
 

返回false不一定是真的在这里;你的孩子的意见之一可能实际上处理的事件,需要让别人知道......我想你需要做更多的东西是这样的:

  @覆盖
公共布尔dispatchTouchEvent(MotionEvent EV){
    gestureDetector.onTouchEvent(EV);

    返回super.dispatchTouchEvent(EV);
}
 

I'm trying to implement Catch onTouch event by parent, handle it and pass to children butt failed to do that.

Gist of full 2d scroll code is here

what I have tried:

Override the method dispatchTouchEvent, and sent the MotionEvent to a GestureDetector

     @Override
     public boolean dispatchTouchEvent (MotionEvent ev) {

    // return super.dispatchTouchEvent(ev);

     boolean result = super.dispatchTouchEvent(ev);
     if (gestureDetector.onTouchEvent(ev)) {
            return result;
       } 
       //If not scrolling vertically (more y than x), don't hijack the event.
        else {
            return false;
       }

}

Activity:

   setContentView(R.layout.activity_fullscreen);

    final View contentView = findViewById(R.id.fullscreen_content);

    android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
    ctx = this;

   ShapeDrawableView     animView = new ShapeDrawableView(this, fm, ctx);//customview



    ((ViewGroup) contentView).addView(animView);

Layout:

<view
   class="com.example.astream.TwoDScrollView" 
   android:id="@+id/fullscreen_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     android:gravity="center" />

解决方案

Can't inspect in great detail right now, but this stands out as an issue:

//If not scrolling vertically (more y than x), don't hijack the event.
else {
    return false;
}

returning false isn't necessarily true here; one of your child views may have actually handled the event and needed to let somebody know... I think you need to do something more like this:

@Override
public boolean dispatchTouchEvent (MotionEvent ev) {
    gestureDetector.onTouchEvent(ev);

    return super.dispatchTouchEvent(ev);
}

这篇关于通过自定义2D滚动型父抓住onTouch事件,处理它,并传给孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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