Android: why setVisibility(View.GONE); or setVisibility(View.INVISIBLE); do not work

前端 未结 11 2125
[愿得一人]
[愿得一人] 2020-12-02 12:51

I want my DatePicker and the button to be invisible in the begining. And when I press my magic button I want to setVisibility(View.Visible);

The problem

11条回答
  •  不思量自难忘°
    2020-12-02 13:44

    Today I had a scenario, where I was performing following:

    myViewGroup.setVisibility(View.GONE);
    

    Right on the next frame I was performing an if check somewhere else for visibility state of that view. Guess what? The following condition was passing:

    if(myViewGroup.getVisibility() == View.VISIBLE) {
        // this `if` was fulfilled magically
    }
    

    Placing breakpoints you can see, that visibility changes to GONE, but right on the next frame it magically becomes VISIBLE. I was trying to understand how the hell this could happen.

    Turns out there was an animation applied to this view, which internally caused the view to change it's visibility to VISIBLE until finishing the animation:

    public void someFunction() {
        ...
        TransitionManager.beginDelayedTransition(myViewGroup);
        ...
    
        myViewGroup.setVisibility(View.GONE);
    }
    

    If you debug, you'll see that myViewGroup indeed changes its visibility to GONE, but right on the next frame it would again become visible in order to run the animation.

    So, if you come across with such a situation, make sure you are not performing an if check in amidst of animating the view.

    You can remove all animations on the view via View.clearAnimation().

提交回复
热议问题