Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: how to expand and collapse cardView size in RecyclerView

Tags:

android

I need to expand and collapse CardView size in RecyclerView at run time. I use RecyclerView to display a list and CardView as RecyclerView Item. As I am new to this concept please help me.

like image 650
Anil Avatar asked Aug 10 '15 04:08

Anil


People also ask

What is expanding cards Java?

An Android library that lets you create in a simple, fast and hassle-free way a CardView in which you can insert your custom layout and just expand and collapse without even writing a single Java/Kotlin line of code. This component follows the Material Design Guidelines.


1 Answers

I have expand on run time. like this,

Setting my view on onCreateView()

descriptionCardView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver
    .OnPreDrawListener() {
@Override
public boolean onPreDraw() {
    detailProductDescription.getViewTreeObserver().removeOnPreDrawListener(this);
    // save the full height
    descriptionViewFullHeight = detailProductDescription.getHeight();

    // initially changing the height to min height
    ViewGroup.LayoutParams layoutParams = descriptionCardView.getLayoutParams();
    layoutParams.height = (int) getActivity().getResources().getDimension(R.dimen
            .product_description_min_height);
    descriptionCardView.setLayoutParams(layoutParams);

    return true;
 }
});

and onClickingcardview

@OnClick(R.id.detail_product_description)
void onProductDescriptionClicked(View view) {
    toggleProductDescriptionHeight();
}

i am setting CardView Height to expand and collapse.

private int descriptionViewFullHeight;    
private void toggleProductDescriptionHeight() {

    int descriptionViewMinHeight = (int) getActivity().getResources().getDimension(R.dimen
            .product_description_min_height);
    if (descriptionCardView.getHeight() == descriptionViewMinHeight) {
        // expand
        ValueAnimator anim = ValueAnimator.ofInt(descriptionCardView.getMeasuredHeightAndState(),
                descriptionViewFullHeight);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int val = (Integer) valueAnimator.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = descriptionCardView.getLayoutParams();
                layoutParams.height = val;
                descriptionCardView.setLayoutParams(layoutParams);
            }
        });
        anim.start();
    } else {
        // collapse
        ValueAnimator anim = ValueAnimator.ofInt(descriptionCardView.getMeasuredHeightAndState(),
                descriptionViewMinHeight);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int val = (Integer) valueAnimator.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = descriptionCardView.getLayoutParams();
                layoutParams.height = val;
                descriptionCardView.setLayoutParams(layoutParams);
            }
        });
        anim.start();
    }
}
like image 109
Abhishek Avatar answered Oct 22 '22 07:10

Abhishek