android View with View.GONE still receives onTouch

2019-01-21 16:01发布

This is confusing me:

As far as I have read, a view with setVisibility(View.GONE); should not receive any more touch- or click events. My layout has two parts, which will be visible or gone so that only one of them is visible and usable at a time but View.GONE doesn't do the trick. I can't see the GONE view, as expected, but it still reacts to onClick (and consumes the event the other view should get).

Can you help me?

Maybe of interest: When I start my project one view is GONE, the other visible. This time it will work as expected (the GONE view is basically ignored), but after setting View.GONE through the code it'll stop working.

9条回答
欢心
2楼-- · 2019-01-21 16:02

Try setting clickable property to false using setClickable(false) after setVisibility(View.GONE)

查看更多
ら.Afraid
3楼-- · 2019-01-21 16:02

For those that did the answers above and still didn't solve their problems, I recommend removing the view from the parent view. If you need display the view again, just make a copy and add it to the parent view.

This might seem overkill but I was hiding / showing whole view groups in your case it might be a button, textview or image, this solution will still work.

查看更多
姐就是有狂的资本
4楼-- · 2019-01-21 16:06

if have a animation at the view, you should call view.clearAnimation.

查看更多
冷血范
5楼-- · 2019-01-21 16:08

I would have post this as a comment, but unfortunately I was not able to post a comment. As it could be a possible solution for you, i post it that way:

As you write "onClick" I assume you're using the onClick attribute in your XML layout. Try to set an OnClickListener with setOnClickListener instead of the onClick attribute. Maybe this helps...

查看更多
Juvenile、少年°
6楼-- · 2019-01-21 16:10

If you set setVisibility(View.GONE) after some animation (fade out, for example), then try clearing the animation with clearAnimation(). This is what helped me.

查看更多
smile是对你的礼貌
7楼-- · 2019-01-21 16:11

Yes,mview.clearAnimation() have some issuses but amination.setFillAfter(false);and mview.setClickable(false); WORKS perfect .

查看更多
登录 后发表回答