android View with View.GONE still receives onTouch and onClick

后端 未结 9 2124
温柔的废话
温柔的废话 2020-12-13 03:40

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

相关标签:
9条回答
  • 2020-12-13 03:47

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

    0 讨论(0)
  • 2020-12-13 03:48

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

    0 讨论(0)
  • 2020-12-13 03:53

    Do you maybe use animations to show/hide the views? I get this behaviour when I use animations that have android:fillEnabled="true" android:fillAfter="true" Don't understand it, and seems like a bug - if I use animations without fillEnabled/fillAfter, all works as expected...

    0 讨论(0)
  • 2020-12-13 03:54

    What I expect is happening is that you make a view invisible, but that views children still respond to clicks (ie your view is a ViewGroup). You could do something along the lines of:

    private void hideTheChildren(View v){
        if(v instanceof ViewGroup) {
            int count = ((ViewGroup)v).getChildCount();
            for(int k = 0 ; k < count ; k++) {
                hideTheChildren(((ViewGroup)v).getChildAt(k));
            }
            v.setVisibility(View.GONE);
        }
        else {
            v.setClickable(false);
            v.setVisibility(View.GONE);
        }
    }
    

    of course then you also have to do the opposite

    private void showTheChildren(View v){
        if(v instanceof ViewGroup) {
            int count = ((ViewGroup)v).getChildCount();
            for(int k = 0 ; k < count ; k++) {
                showTheChildren(((ViewGroup)v).getChildAt(k));
            }
            v.setVisibility(View.VISIBLE);
        }
        else {
            v.setClickable(true);
            v.setVisibility(View.VISIBLE);
        }
    }
    

    This has worked for me in the past. I don't currently know of a better way to do this.

    0 讨论(0)
  • 2020-12-13 03:57

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

    0 讨论(0)
  • 2020-12-13 03:59

    try adding .clearAnimation() in the onAnimationEnd override.

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