Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
372 views
in Technique[技术] by (71.8m points)

android - How to handle click in the child Views, and touch in the parent ViewGroups?

In my layout I have a structure like that:

--RelativeLayout
  |
  --FrameLayout
    |
    --Button, EditText...

I want to handle touch events in the RelativeLayout and in the FrameLayout, so I set the onTouchListener in these two view groups. But only the touch in the RelativeLayout is captured.

To try solve this, I wrote my own CustomRelativeLayout, and override the onInterceptTouchEvent, now the click in the child ViewGroup (FrameLayout) is captured, but the click in the buttons and other views doesn't make any effect.

In my own custom layout, I have this:

public boolean onInterceptTouchEvent(MotionEvent ev) {
    return true;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to override the onInterceptTouchEvent() for each child, otherwise it will remain an onTouchEvent for the parent.

Intercept Touch Events in a ViewGroup

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    /*
    * This method JUST determines whether we want to intercept the motion.
    * If we return true, onTouchEvent will be called and we do the actual
    * scrolling there.
    */
...
    // In general, we don't want to intercept touch events. They should be 
    // handled by the child view.
    return false;
}

You need to return false to have the child handle it, otherwise you are returning it to the parent.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...