Passing touch events to the parent view

后端 未结 9 1248
情书的邮戳
情书的邮戳 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: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.

提交回复
热议问题