Catch onTouch event by Custom 2d Scrollview parent, handle it and pass to children

你。 提交于 2019-12-25 00:09:07

问题


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" />

回答1:


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);
}


来源:https://stackoverflow.com/questions/26354263/catch-ontouch-event-by-custom-2d-scrollview-parent-handle-it-and-pass-to-childr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!