0

I have a RecyclerView that contains a list of MaterialCardView. Inside MaterialCardView, there is a RecyclerView and an Expand / Collapse button. When clicking on Collapse, the internal RecyclerView is assigned Visible.GONE, but on devices with API <24, the change in the height of the cardview is not correctly processed

[See API19|API22|API23|API29] (http://g.recordit.co/08OJWDaOja.gif)

`` `

TransitionManager.beginDelayedTransition(recyclerView, new AutoTransition());
        if(state){
            mProfileList.setVisibility(View.VISIBLE);
            mExpand.setText(R.string.item_group_collapse);
            managerProfiles.updateGroupState(group, true);
        }else{
            mProfileList.setVisibility(View.GONE);
            mExpand.setText(R.string.item_group_expand);
            managerProfiles.updateGroupState(group, false);
        }

`` `

Sergey J.
  • 1
  • 2

1 Answers1

0

This is a bug in the foreground drawable of the MaterialCardView, i've created a bug report here: https://github.com/material-components/material-components-android/issues/537

I've created the following quick fix:

public class CardViewForegroundFix extends LayerDrawable {
    public CardViewForegroundFix(Drawable foreground) {
        super(new Drawable[]{foreground});
    }

    @Override
    public int getMinimumHeight() {
        return -1;
    }

    @Override
    public int getMinimumWidth() {
        return -1;
    }

    public static void fix(MaterialCardView cardView) {
        Drawable foreGround = cardView.getForeground();
        if (!(foreGround instanceof CardViewForegroundFix)) {
            final CardViewForegroundFix foregroundFix = new CardViewForegroundFix(foreGround);
            cardView.setForeground(foregroundFix);
            // foreGround#callback is nullified in cardView#setForeground,
            // reset it to the foreground fix drawable
            foreGround.setCallback(foregroundFix);
        }
    }
}

Which you can apply by CardViewForegroundFix.fix(materialCardViewInstance)

Roy
  • 648
  • 9
  • 17