0

I have a RecyclerView in my android project, it has 2 LinearLayout in each row. first I want that one of those layouts got Gone. after I click on row it get visible with animation. in short I want to implement a expandable RecyclerView without any external libraries. can you give me a solution to do this? I wanted to achieve this by animating hoder.itemView 's Height. but actually I didn't know what to insert instead of???? because at first I don't know it's height.

if(holder.layout2.getVisibility() == View.Gone) {
            holder.layout2.setVisibility( View.VISIBLE );
            animateHeight(holder.itemView,?????);
        }
        else {
            animateHeight(holder.itemView,holder.itemView.getHeight - holder.layout2.getHeight());
            holder.layout2.setVisibility( View.GONE );
        }

public void animateHeight(final View v, final int height) {

    final int initialHeight = v.getMeasuredHeight();
    int duration = 500;
    Interpolator interpolator = new AccelerateInterpolator();

    // I have to set the same height before the animation because there is a glitch
    // in the beginning of the animation
    v.getLayoutParams().height = initialHeight;
    final int diff = height - initialHeight;
    v.requestLayout();

    Animation a = new Animation() {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            v.getLayoutParams().height = initialHeight + (int) (diff * interpolatedTime);
            v.requestLayout();
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    a.setDuration(duration);
    a.setInterpolator(interpolator);
    v.startAnimation(a);
}
Maryam
  • 883
  • 10
  • 24
  • Can you try like this instead of the above one, make the parent layout of your child view as linear layout and apply `android:animateLayoutChanges="true" ` to it. Now you can show and hide it with an animation!! – Aldrin Joe Mathew Apr 26 '17 at 08:17
  • @AldrinMathew , I tried it but nothing happened . and I want to access how to animate it like duration – Maryam Apr 26 '17 at 08:24
  • 1
    possible duplicate http://stackoverflow.com/questions/22454839/Android%20adding%20simple%20animations%20while%20setvisibility(view.Gone)/22454966 – Rajesh N Apr 26 '17 at 09:09

1 Answers1

0

Do something like this in your view holder class to get the height of each child,

itemView.getViewTreeObserver().addOnGlobalLayoutListener( 
    new OnGlobalLayoutListener(){

        @Override
        public void onGlobalLayout() {

            mHeight = mView.getHeight();
            mView.getViewTreeObserver().removeGlobalOnLayoutListener( this );
            mView.setVisibility( View.GONE );
        }

});

This will be the height of the child in expanded mode, you can use it later when you're trying to animate the view.

Aldrin Joe Mathew
  • 472
  • 1
  • 4
  • 13