Crop and display a child while allowing child to take the full height of its content by setting height as wrap_content?
Asked Answered
S

1

6

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.

Shinn answered 30/1, 2018 at 6:27 Comment(1)
Please paste the code hereLacreshalacrimal
P
2

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.

Priory answered 2/2, 2018 at 8:50 Comment(5)
The LinearLayout still returns 150dp(height of the cardview) when called getHeight() although now it visually crops and displays the content. But the whole purpose of this is to get the would be height of the LinearLayout since I need it to smoothly Animate to give the effect of expanding the collapsed. I intend to make the animation by increasing the height of the CardView to the would be value of the LinearLayout smoothly within a small time. @PrioryShinn
you can try to set android:animateLayoutChanges="true" on cardView, but I am not sure how it will behave, and then change layout params of LinearLayout inside cardViewPriory
though i can do this i cannot figure out the final height i need to enlarge the CardView upto without knowing the full height required by the LinearLayout @PrioryShinn
What if card view have height=wrap_contentPriory
I want the CardView to be collapsible. It should display only the top (say the top 150dp) of the whole thing until it is expanded by the user. Then once the user expands the card (by clicking or other etc.), it should go through an animation (say 400milisecond total time duration) which expands the card from 150dp(in the begining of the animation) to the height of the child (in the end of the animation). To get the "height of the child" i need the child to return its actual height if it were allowed to take the maximum height needed to wrap its content when it is called getHeight() . @PrioryShinn

© 2022 - 2024 — McMap. All rights reserved.