Skip to content

Instantly share code, notes, and snippets.

@drewhannay
Created December 15, 2014 17:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save drewhannay/0b5a3217d637efcddd2b to your computer and use it in GitHub Desktop.
Save drewhannay/0b5a3217d637efcddd2b to your computer and use it in GitHub Desktop.
ExpandableItemViewHolder
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