Вопрос

I want to have an overlay view to receive gesture events but still keep receiving onTouch event of parent(below) view. However, The gesture in child view seems hide parent onTouch event.

Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
    <ParentView

            android:background="#FFFFFF"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

        <ChildView android:background="#FF4433"
                   android:orientation="horizontal"
                   android:layout_width="fill_parent"
                   android:layout_height="200dp" android:layout_marginTop="100dp">

        </ChildView>

    </ParentView>
</LinearLayout>

Parent View:

public class ParentView extends FrameLayout implements View.OnTouchListener {
    public ParentView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        Logger.write("Parent is touching");
        return false;  
    }
}

Client View:

public class ChildView extends FrameLayout implements View.OnTouchListener {
    private GestureDetector myGestureDetector;
    private boolean isTapping;

    public ChildView(Context context, AttributeSet attrs) {
        super(context, attrs);
        myGestureDetector=new  GestureDetector(context, new MyGestureDetector());
        setOnTouchListener(this);
    }


    @Override
    public boolean onTouch(View view, MotionEvent event) {
        return  myGestureDetector.onTouchEvent(event);
    }

    class MyGestureDetector extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onDown(MotionEvent e) {
            return true;    
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            try {
                final ViewConfiguration vc = ViewConfiguration.get(getContext());
                final int swipeMinDistance = vc.getScaledPagingTouchSlop();
                final int swipeMaxOffPath = vc.getScaledTouchSlop();
                final int swipeThresholdVelocity = vc.getScaledMinimumFlingVelocity() / 2;
                if (Math.abs(e1.getY() - e2.getY()) > swipeMaxOffPath) {
                    return false;
                }

                // right to left swipe
                if (e2.getX() - e1.getX() > swipeMinDistance && Math.abs(velocityX) >
                        swipeThresholdVelocity) {
                    Logger.write("Swipe!!!!!!!!!!!!!!!!!!!!!!!");

                }
            } catch (Exception e) {
                // nothing
            }

            return false;
        }
    }
}
Это было полезно?

Решение

Implement GestureDetector in parent not in child view, otherwise you would need n detectors if parent has n child views.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top