Android - Expandable TextView with Animation

后端 未结 15 1062
小蘑菇
小蘑菇 2020-11-28 18:24

I have a TextView which firstly shows a small portion of a long text.

The user can press a \"see more\" button to expand the TextView and s

15条回答
  •  北海茫月
    2020-11-28 19:07

    Smooth expanding (using heigh & ObjectAnimator)
    FYI: requires API 11

    public static void expandCollapsedByMaxLines(@NonNull final TextView text) {
        final int height = text.getMeasuredHeight();
        text.setHeight(height);
        text.setMaxLines(Integer.MAX_VALUE); //expand fully
        text.measure(View.MeasureSpec.makeMeasureSpec(text.getMeasuredWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, View.MeasureSpec.UNSPECIFIED));
        final int newHeight = text.getMeasuredHeight();
        ObjectAnimator animation = ObjectAnimator.ofInt(text, "height", height, newHeight);
        animation.setDuration(250).start();
    }
    

    P.S. I assume TextView limited by maxLines.
    P.S.S. Thanks Amagi82 for ObjectAnimator example

提交回复
热议问题