我使用CardView作为Recycler Adapter的项目。
首先,所有项目都不会显示整个内容,(我将其剪切并放入' ......等等) 但单击它后,单击的项目将缩放其高度以适合内容高度。 (这里我将文本设置为整个内容,并希望卡可以缩放以适应它)
看起来像:
我知道如何将高度设置为特定目标高度的动画。 但在这种情况下,我不知道如何测量显示整个内容所需的高度,每个项目应该有不同的目标高度。
我该怎么做?
答案 0 :(得分:0)
你可以做的是让View
测量自己,不对其高度施加任何限制。请注意拨打view.getWidth()
的电话,只有在View
布局后才能这样做,因为您在onClick()
中调用它应该没问题。
int widthSpec = View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
view.measure(widthSpec, heightSpec);
int targetHeight = view.getMeasuredHeight();
假设您的View是设置了这些属性的TextView:
android:layout_height="wrap_content"
android:maxLines="1"
android:ellipsize="end"
完整的例子是
// this is the height measured with maxLines 1 and height
// to wrap_content
final int startHeight = view.getHeight();
// you want to measure the TextView with all text lines
view.setMaxLines(Integer.MAX_VALUE);
int widthSpec = View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
view.measure(widthSpec, heightSpec);
// final height of the TextView
int targetHeight = view.getMeasuredHeight();
// this is the value that will be animated from 0% to 100%
final int heightSpan = targetHeight-startHeight;
// remove that wrap_content and set the starting point
view.getLayoutParams().height = startHeight;
view.setLayoutParams(view.getLayoutParams());
Animation animation = new Animation(){
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
view.getLayoutParams().height = (int) (startHeight + heightSpan*interpolatedTime);
view.setLayoutParams(view.getLayoutParams());
}
};
animation.setDuration(1000);
view.startAnimation(animation);
答案 1 :(得分:0)
int minLineNumber = 2;
textView.setMaxLines(minLineNumber); // 2 is your number of line for the first time
点击它时:
textView.setMaxLines(Integer.MAX_VALUE); // set the height like wrap_content
如果你想要动画,你就是这样的:
ObjectAnimator animation = ObjectAnimator.ofInt(
textView,
"maxLines",
textView.getLineCount());
int duration = (textView.getLineCount() - minLineNumber) * 50;
animation.setDuration(duration);
animation.start();
如果TextView中的文字太多,最好使用固定的持续时间,而不是依赖于身高。