我正在致力於在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。 –
讓視圖可見性可見/消失是個好主意。現在我不這樣做。那麼,有什麼辦法可以動態地達到目標高度。 –