Frage

I'm having a FrameLayout that has an extended ImageView (github) as a child. When I set an onClick()-Event to the FrameLayout it won't be triggered. The reason appears to be the onTouch() method's return value.

If I set the ACTION_DOWN's return value to false the event is passed along properly - but then the Multitouch functionalities break. Also running performClick() in the ACTION_UP event comes to nothing.

How to handle those events correctly?

War es hilfreich?

Lösung

Currently I solved the issue by manually passing the click event to the view's parents:

new GestureDetector( context, new GestureDetector.SimpleOnGestureListener()
{
    @Override
    public boolean onSingleTapConfirmed( MotionEvent event )
    {
        View view = MultitouchImageView.this;

        if( !performClick() )
        {
            while( view.getParent() instanceof View )
            {
                view = (View) view.getParent();

                if( view.performClick() )
                {
                    return true;
                }
            }

            return false;
        }

        return true;
    }

    ... 
}

Doing the same thing for an occuring LongPress event. It works the way I need it but I don't like the solution..

Andere Tipps

Try setting the parent FrameLayout to receive touches before the ImageView child. You can do this by adding

android:descendantFocusability="beforeDescendants"

to the xml of the parent.

e.g.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:descendantFocusability="beforeDescendants">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</FrameLayout>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top