-
-
Save CMingTseng/b7bbfc2c39134a4a72307c540d4142b4 to your computer and use it in GitHub Desktop.
Simple Expand / Collapse RecyclerView Item
This file contains 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
public static class ExampleViewHolder extends RecyclerView.ViewHolder | |
implements View.OnClickListener { | |
private int originalHeight = 0; | |
private boolean isViewExpanded = false; | |
private YourCustomView yourCustomView | |
public ExampleViewHolder(View v) { | |
super(v); | |
v.setOnClickListener(this); | |
// Initialize other views, like TextView, ImageView, etc. here | |
// If isViewExpanded == false then set the visibility | |
// of whatever will be in the expanded to GONE | |
if (isViewExpanded == false) { | |
// Set Views to View.GONE and .setEnabled(false) | |
yourCustomView.setVisibility(View.GONE); | |
yourCustomView.setEnabled(false); | |
} | |
} | |
@Override | |
public void onClick(final View view) { | |
// If the originalHeight is 0 then find the height of the View being used | |
// This would be the height of the cardview | |
if (originalHeight == 0) { | |
originalHeight = view.getHeight(); | |
} | |
// Declare a ValueAnimator object | |
ValueAnimator valueAnimator; | |
if (!mIsViewExpanded) { | |
yourCustomView.setVisibility(View.VISIBLE); | |
yourCustomView.setEnabled(true); | |
mIsViewExpanded = true; | |
valueAnimator = ValueAnimator.ofInt(originalHeight, originalHeight + (int) (originalHeight * 2.0)); // These values in this method can be changed to expand however much you like | |
} else { | |
mIsViewExpanded = false; | |
valueAnimator = ValueAnimator.ofInt(originalHeight + (int) (originalHeight * 2.0), originalHeight); | |
Animation a = new AlphaAnimation(1.00f, 0.00f); // Fade out | |
a.setDuration(200); | |
// Set a listener to the animation and configure onAnimationEnd | |
a.setAnimationListener(new Animation.AnimationListener() { | |
@Override | |
public void onAnimationStart(Animation animation) { | |
} | |
@Override | |
public void onAnimationEnd(Animation animation) { | |
yourCustomView.setVisibility(View.INVISIBLE); | |
yourCustomView.setEnabled(false); | |
} | |
@Override | |
public void onAnimationRepeat(Animation animation) { | |
} | |
}); | |
// Set the animation on the custom view | |
yourCustomView.startAnimation(a); | |
} | |
valueAnimator.setDuration(200); | |
valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator()); | |
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { | |
public void onAnimationUpdate(ValueAnimator animation) { | |
Integer value = (Integer) animation.getAnimatedValue(); | |
view.getLayoutParams().height = value.intValue(); | |
view.requestLayout(); | |
} | |
}); | |
valueAnimator.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment