LOG_TAG
LOG_TAG

Reputation: 20569

android view animate and without animation acts differently

when I use case 1 it works as expected , but use animate (case2) the view never becomes visible!

case 1>

viewstub.setVisibility(View.GONE);
mContentLoaded = false;
showContentOrLoadingIndicator(mContentLoaded);  

in this View will become invisible and visible again!

case 2>

viewstub.setVisibility(View.GONE); 

viewstub.animate().alpha(0f).setDuration(mShortAnimationDuration)
            .setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    viewstub.setVisibility(View.GONE);
                }
            });

in this View will become invisible and never becomes visible again! any solutions or reason ?

private void showContentOrLoadingIndicator(boolean contentLoaded) {
        // Decide which view to hide and which to show.
        final View showView = contentLoaded ? listView : mLoadingView;
        final View hideView = contentLoaded ? mLoadingView : listView;

        // Set the "show" view to 0% opacity but visible, so that it is visible
        // (but fully transparent) during the animation.
        showView.setAlpha(0f);
        showView.setVisibility(View.VISIBLE);

        // Animate the "show" view to 100% opacity, and clear any animation
        // listener set on
        // the view. Remember that listeners are not limited to the specific
        // animation
        // describes in the chained method calls. Listeners are set on the
        // ViewPropertyAnimator object for the view, which persists across
        // several
        // animations.
        showView.animate().alpha(1f).setDuration(mShortAnimationDuration)
                .setListener(null);

        // Animate the "hide" view to 0% opacity. After the animation ends, set
        // its visibility
        // to GONE as an optimization step (it won't participate in layout
        // passes, etc.)
        hideView.animate().alpha(0f).setDuration(mShortAnimationDuration)
                .setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        hideView.setVisibility(View.GONE);
                    }
                });
    }

Upvotes: 0

Views: 488

Answers (2)

Dude
Dude

Reputation: 1302

I don't fully understand the exact reason and haven't really delved deep into the docs, but I think I can point you in the right direction.

It seems that the issue comes from this part of the code:

showView.animate().alpha(1f).setDuration(mShortAnimationDuration)
            .setListener(null);

It's as if the animation may run and finish and only then set the listener to null(which contradicts the auto-start property. So if your list has an AnimatorListenerAdapterset to it, when calling showContentOrLoadingIndicator(true); then the

@Override
public void onAnimationEnd(Animator animation) {
    hideView.setVisibility(View.GONE);
}

method will hide your list before the listener is set to null. I found this by logging the view that gets set to GONE in onAnimationEnd

So the solution would be to set the listener to null earlier in the method chain:

showView.animate()
    .setListener(null)
    .alpha(1f)
    .setDuration(mShortAnimationDuration)

If someone can offer detailed explanations about how this can happen, I'd be interested to get further educated.

Hope this helps

Upvotes: 1

ThaMe90
ThaMe90

Reputation: 4296

View.Gone (source) sets the view to be gone from the UI. It's still there, but any other UI elements are displayed over it. The disappeared view does not take up any space.

To show it again, you should set the visibility back to View.Visible (source).

Upvotes: 0

Related Questions