Last active
January 30, 2022 02:31
-
-
Save ZkHaider/9bf0e1d7b8a2736fd676 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
This code works perfectly, could you please add some more comments explaining why and how it works?