Passing touch events to the parent view

后端 未结 9 1063
情书的邮戳
情书的邮戳 2020-11-27 05:25

I have a custom ViewSwitcher in which I implemented touch events so I am able to scroll through screens using the touchscreen.

My layout hierarchy looks

相关标签:
9条回答
  • 2020-11-27 05:44

    In my parent layout, the only way I have found to prevent the child from capturing the touch event is by overriding onInterceptTouchEvent to return true.

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev)
    {
        return true;
    }
    
    0 讨论(0)
  • 2020-11-27 05:48

    Thank you everyone for answering the question. But I was able to figure it out in a much simpler manner. Since my ViewSwitcher wasn't detecting the touch event, I intercepted the touch event, called the onTouchEvent() and returned false. Here:

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev)
    {
        onTouchEvent(ev);
        return false;
    }
    

    By overriding the onInterceptTouchEvent(), I was able to intercept the touch event in the activity. Then I called the onTouchEvent() in the ViewSwitcher which handles the switching of the ListViews. And finally by returning false, it makes sure that the ViewGroup doesn't consume the event.

    0 讨论(0)
  • 2020-11-27 05:52

    I don't think there is an easy way for you to do this.

    It's not that complicated, but you will need to create your own view that extends the ListView. Then you can override the onTouch handler and decide (depending on the touch event) whether you want to handle it (and return true) or pass it down to the parent View.

    The problem also is that once a View handles a touch event, it is the one that will get all the remaining ones...

    From the Android documentation :

    onTouch() - This returns a boolean to indicate whether your listener consumes this event. The important thing is that this event can have multiple actions that follow each other. So, if you return false when the down action event is received, you indicate that you have not consumed the event and are also not interested in subsequent actions from this event. Thus, you will not be called for any other actions within the event, such as a finger gesture, or the eventual up action event

    So, for example, if you want to have vertical move to scroll through the list and during the same touch event (without lifting your finger), you want horizontal move to switch the views, that's going to be quite challenging.

    But if you can use gestures for example or handle everything in your custom view and depending on what the MotionEvent is, send commands to the ViewSwitcher.

    0 讨论(0)
  • 2020-11-27 05:52

    If your view wants to pass the event up, make sure you return false in onTouchEvent. Otherwise, the platform thinks you consumed the event and no further processing is needed.

    0 讨论(0)
  • 2020-11-27 05:56

    Did you try setting the ListView items as non-clickable like this: listView.setClickable(false); This should propogate the click event upwards.

    0 讨论(0)
  • 2020-11-27 05:56

    what i did is to set a toucheventlistener on the listview in the viewswitcher, handles the event, detects the fling action and call the metchod of viewswitcher. it works.

    0 讨论(0)
提交回复
热议问题