Android - Expandable TextView with Animation

后端 未结 15 1072
小蘑菇
小蘑菇 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 18:54

    You can use the new TransitionManager for the animation and calling the maxLines attribute to set the amount

    fun toggleReadMoreTextView(linesWhenCollapsed: Float) {
        if (viewDataBinding.textView.maxLines != Integer.MAX_VALUE) {
            // exapand
            viewDataBinding.textView.maxLines = Integer.MAX_VALUE
        } else {
            // collapse
            viewDataBinding.textView.maxLines = linesWhenCollapsed
        }
        // start animation
        TransitionManager.beginDelayedTransition(viewDataBinding.constraintLayout)
    }
    

提交回复
热议问题