Created
December 15, 2014 17:50
-
-
Save drewhannay/0b5a3217d637efcddd2b to your computer and use it in GitHub Desktop.
ExpandableItemViewHolder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.animation.ValueAnimator; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.View; | |
import android.view.animation.LinearInterpolator; | |
public abstract class ExpandableItemViewHolder extends RecyclerView.ViewHolder { | |
private int mOriginalHeight; | |
private boolean mIsViewExpanded; | |
public ExpandableItemViewHolder(View itemView) { | |
super(itemView); | |
itemView.setOnClickListener(mOnClickListener); | |
} | |
abstract int getExpandedHeight(int originalHeight); | |
private final View.OnClickListener mOnClickListener = new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if (mOriginalHeight == 0) { | |
mOriginalHeight = itemView.getHeight(); | |
} | |
ValueAnimator valueAnimator; | |
if (!mIsViewExpanded) { | |
mIsViewExpanded = true; | |
valueAnimator = ValueAnimator.ofInt(mOriginalHeight, getExpandedHeight(mOriginalHeight)); | |
} else { | |
mIsViewExpanded = false; | |
valueAnimator = ValueAnimator.ofInt(getExpandedHeight(mOriginalHeight), mOriginalHeight); | |
} | |
valueAnimator.setDuration(300); | |
valueAnimator.setInterpolator(new LinearInterpolator()); | |
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { | |
public void onAnimationUpdate(ValueAnimator animation) { | |
itemView.getLayoutParams().height = (Integer) animation.getAnimatedValue(); | |
itemView.requestLayout(); | |
} | |
}); | |
valueAnimator.start(); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment