我正在致力于在CardView
上展开和收缩属性。CardView的可扩展高度
public class SimpleCardView extends CardView {
private int animationDuration;
public SimpleCardView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public SimpleCardView(Context context) {
super(context);
}
public SimpleCardView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void expand(){
final int initialHeight = getHeight();
final float scale = getContext().getResources().getDisplayMetrics().density;
int targetHeight = (int) (232 * scale + 0.5f);
final int distance_to_expand = targetHeight - initialHeight;
Animation animation = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
getLayoutParams().height = (int) (initialHeight +(distance_to_expand*interpolatedTime));
requestLayout();
}
@Override
public boolean willChangeBounds() {
return true;
}
};
animationDuration = distance_to_expand;
animation.setDuration((long)distance_to_expand);
startAnimation(animation);
}
public int getAnimationTime(){
return animationDuration;
}
public void collapse(){}
}
这是我的截图:
我设定目标高度的恒定值。
int targetHeight = (int) (232 * scale + 0.5f);
这里,targetHeight
是CardView
一个膨胀高度。 因此,当内容太长时,只显示少部分内容。
有什么办法来动态设置高度不是一个常数值吗?
简单的解决方案兄弟。设置CardView高度wrap_content setYour标题当你做和下面的TextView内容可见性应该消失。所以当你点击展开只是试图看到包含内容的TextView。 –
让视图可见性可见/消失是个好主意。现在我不这样做。那么,有什么办法可以动态地达到目标高度。 –