【发布时间】:2017-07-10 14:52:15
【问题描述】:
我正在处理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 Title 正如你所做的那样,TextView 下面的内容可见性应该消失了。所以当你点击展开时,试着看到包含内容的 TextView。
-
让视图可见性可见/消失是个好主意。现在我不那样做。那么,有什么方法可以动态设定目标高度。