LOG_TAG
LOG_TAG

Reputation: 20589

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

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

Upvotes: 0

Views: 1411

Answers (1)

JRaymond
JRaymond

Reputation: 11782

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

Upvotes: 2

Related Questions