0
votes

Working on this app in which I'am trying to manage show/hide animation on a LinearLayout.

The problem I have is, on toggling the visibility it seems to hide any children the second time..

To make my story more clear here a screenshot:

  • on start - hidden
  • 1st click - shows linear layout with children (textview)
  • 2th click - hids linear layout again
  • 3th click - shows linear layout but no children (textview)

enter image description here

Layoutfile:

        <TextView
            android:id="@+id/tv_lecture"
            style="@style/Text.Primary.Light"
            android:singleLine="true" />

        <LinearLayout
            android:visibility="gone"
            android:id="@+id/grades_container"
            style="@style/Container.Vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="10"/>
        </LinearLayout>

code:

private void animate(final LinearLayout gradesContainer) {
    if (gradesContainer.getVisibility() == View.GONE) {
        gradesContainer.animate()
                .translationY(gradesContainer.getHeight())
                .alpha(1.0f)
                .setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        super.onAnimationStart(animation);
                        gradesContainer.setVisibility(View.VISIBLE);
                        gradesContainer.setAlpha(0.0f);
                    }
                });
    }
    else {
        gradesContainer.animate()
                .translationY(0)
                .alpha(0.0f)
                .setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        gradesContainer.setVisibility(View.GONE);
                    }
                });
    }
}

On lecture TextView click will trigger the method: animate()

So whats the problem here? Can't figure it out thats why I'am asking you guys. Thanks!

1
As the children's height can not overflow the parent, I suggest reset child's hieght after expansion animation.Ali Sheikhpour
One question. Your animation works, but have you considered using the animateLayoutChanges instead?AL.
@AliSheikhpour Thanks for the tip. Ill try that out later on, after testing Ill respond the resultJim Vercoelen
@McAwesomville hey thanks for advice. Had to change a lot but it seems to work just fine! If you add answer Ill approveJim Vercoelen
Hey Jim. You're welcome. Happy coding!. :DAL.

1 Answers

1
votes

If you are aiming for just the simple animation. You can just go on ahead and use animateLayoutChanges in your xml.

Cheers! :)