My child is a LinearLayout having a dynamic height, it can change during runtime, could includes images and consists of varying number of child views(which too might have children).
This child LinearLayout is inside a CardView. Now I want this card view to be collapsible. In other words when collapsed it needs to have a smaller height (say 150dp). In this mode the card should display only the top 150dp(minus padding) of the child LinearLayout's content. But the LinearLayout should be allowed to take the maximum size(though the bottom below the CardView's boundary is not shown, cropped) by setting its height as wrap_content. This is similar to what would happen if I had placed a ScrollView(when scrolled to the top) instead of a CardView. But when I do this with CardView. The LinearLayout does not take the maximum height to wrap its children's height instead it takes the height bounded by its Parent CardView.
This causes images to be scaled smaller and could result in many unexpected errors.
Is there a way to achieve the functionality? Is there a different kind of Layout other than cardview that directly support croping and displaying only part of its content while making the child feel like it can heighten indefinitely.
Extend LinearLayout
you are using in CardView
with other class (e.g. MyFrameLayout
) and override method:
@Override
protected void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) {
super.measureChildWithMargins(child, parentWidthMeasureSpec, widthUsed, View.MeasureSpec.UNSPECIFIED, heightUsed);
}
Then use it in layout, and during runtime you can change height param.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With