Search code examples
androidandroid-layoutandroid-recyclerviewlayout-animation

Animate when layout change occurs


In my android application activity layout I have a LinearLayout and RecyclerView, LinearLayout contains an EditText and TextField , and RecyclerView lies below the LinearLayout.

<LinearLayout 
  android:orientation="vertical">
  <LinearLayout 
    android:orientation="vertical">
     <EditText>
     <TextView>
  </LinearLayout>
  <RecyclerView/>
</LinearLayout>

at some point I have to remove the LinearLayout lies above the RecyclerView. so I am hiding that by giving some animaton effects

LinearLayout.animate().translationY(-LinearLayout.getHeight()).setInterpolator(new DecelerateInterpolator()).alpha(0.0f).setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                LinearLayout.setVisibility(View.GONE);
            }
        }).setDuration(HEADER_HIDING_ANIMATION_DURATION);

when the animation ends the view is setting to GONE. then the below RecyclerView is jumping to the top, it simply jumps without any animation and all, is there any way to manage it ? a small animaton for the layout change may help me. I have tried in xml, but it throws some error.


Solution

  • There is one simple way to animate layout changes. Just put to the xml next attribute

    <LinearLayout 
        android:animateLayoutChanges="true"  <====
        android:orientation="vertical">
        <LinearLayout 
            android:orientation="vertical">
            <EditText>
            <TextView>
        </LinearLayout>
        <RecyclerView/>
    </LinearLayout>
    

    Then change the visibility of second LinearLayout to GONE/VISIBLE in code.

    More info could be found here: link to Android Docs