directory

preface

Company projects need a similar WeChat friends text expansion pack up effect, in order to facilitate the beginning I find a similar effect to realize on the net, the results showed that the code quantity are many large amount of calculation, and my mind is so simple so it seems more difficult so I used very simple way to write a custom start up the control.

Results show

Realize the principle of

There are three TextViews, one for displaying text, one for counting lines of text, and one for expanding and collapsing text:

<? The XML version = "1.0" encoding = "utf-8"? > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <! <TextView android:id="@+id/ view_seemore_tvContent "Android :textColor="#000" Android :maxLines="2" android:ellipsize="end" android:textSize="15sp" android:layout_width="match_parent" android:layout_height="wrap_content"/> <! -- Hiding the position, and setting the height very small, <TextView android:id="@+id/view_seemore_tvlinecount" Android :textColor="#000" Android :visibility="invisible" android:textSize="15sp" android:layout_width="match_parent" android:layout_height="1dp"/> <! <TextView android:id="@+id/view_seemore_tv_seemore" Android :layout_marginTop="5dp" Android :text=" View more" android:textColor="#00f" android:visibility="gone" android:maxLines="2" android:ellipsize="end" android:textSize="15sp" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>Copy the code

Then you need to do the corresponding processing in the class of the custom control:

Class SeeMoreView:FrameLayout {private var mIsShowAll:Boolean = false constructor(context: context) : this(context,null) constructor(context: Context, attrs: AttributeSet?) : this(context, attrs,0) constructor(context: Context, attrs: AttributeSet? , defStyleAttr: Int) : super( context, attrs, defStyleAttr ){ View.inflate(context, R.layout.view_seemore,this) initListener()} private fun initListener() {// View more click events View_seemore_tv_seemore. SetOnClickListener {the if (mIsShowAll) {/ / it is the key to the fold code, View_seemore_tvcontent. maxLines = 2 view_seemore_tv_seemore.text = "See more"}else{// This is the key code to seemore, View_seemore_tvcontent. maxLines = 20 view_seemore_tv_seemore.text = "fold up"} mIsShowAll =! MIsShowAll} //attachedToWindow after the operation post {// we have to say this, this is the operation after attachedToWindow, Log.e(" test ","OnLayout${view_seemore_tvlinecount.lineCount}") if(view_seemore_tvLinecount. lineCount>2){log.e (" test ","OnLayout${view_seemore_tvLinecount. lineCount}") { View_seemore_tv_seemore. visibility = view. VISIBLE}else{view_seemore_tv_seemore.visibility = view. GONE}}} /** * Sets the text */ fun setText(text:String){view_seemore_tvContent.text = text view_seemore_tvlinecount.text = text View_seemore_tv_seemore. text = "See more" view_seemore_tvContent. maxLines = 2 mIsShowAll = false if(view_seemore_tvlinecount.lineCount>2){ view_seemore_tv_seemore.visibility = View.VISIBLE }else{ view_seemore_tv_seemore.visibility = View.GONE } } }Copy the code

Note: This code is written for novice use. Old hands suggest a different implementation.

Program source code

Github.com/dahui888/Se…