As shown in the picture:
1. The first step is to create a new CollapsibleTextView class that inherits from LinearLayout:
/** * @author CJF */ public class CollapsibleTextView extends LinearLayout { /** * Default (expanded and hidden) text color blue */ private ForegroundColorSpan expandAndHideColorSpan = new ForegroundColorSpan(Color.parseColor("#3b76ec")); / ** * The default (content text) text color is black */ private ForegroundColorSpan textColorSpan = new ForegroundColorSpan(Color.parseColor("#000000")); /** * The text displays the maximum number of lines, the default is 1 */ private int maxLines = 1; /** * flag */ private boolean flag, flag2, flag3; /** * text */ private String text; /** * Text length */ private int length; private TextView mCollapsibleTextView; public CollapsibleTextView(Context context) { super(context); } public CollapsibleTextView(Context context, AttributeSet attrs) { super(context, attrs); View view = inflate (context, R.layout.layout_collapsible_textview, this); mCollapsibleTextView = (TextView) view.findViewById(R.id.mCollapsibleTextView); //Set textView to be clickable mCollapsibleTextView.setMovementMethod(LinkMovementMethod.getInstance()); } /** * Provide external exposure The method is to set the text content, the maximum number of text display lines, (content text) text color, (expand and hide) text color * of lines * @param textColor (content text) text color * @param expandAndHideColor (expand and hide) text color */ public void setText(String text, int maxLines, int textColor, int expandAndHideColor) { this.text = text; //Assignment text display The maximum number of lines, the minimum is 1 this.maxLines = Math.max(maxLines, 1); //Get the length of the string length = text.length(); //setText calls to set the flag to true flag = true; // When flag3 is false, set the maximum number of lines once if (!flag3) { //Set the default maximum number of lines mCollapsibleTextView.setMaxLines(maxLines); } //set the text mCollapsibleTextView.setText(text); //Set (content text) text color textColorSpan = new ForegroundColorSpan(ContextCompat.getColor(getContext(), textColor)); //Set (expand and hide) text color expandAndHideColorSpan = new ForegroundColorSpan(ContextCompat.getColor(getContext(), expandAndHideColor)); //Re-request layout requestLayout(); } /** *Text setting * Set expand or hide * * @param count * @param str * @return */ private SpannableString getClickableSpan(int count, String str) { //Create a new spannableString (set font color, monitor click, font size) SpannableString span = new SpannableString(str); //set text color when count == 0 and return if (count == 0) { //Set font size for content text span.setSpan(new RelativeSizeSpan(1f), 0, str.length(), Spannable .SPAN_EXCLUSIVE_EXCLUSIVE); //Content text setting font can be clicked and listen span.setSpan(new TextListener(), 0, str.length(), Spanned.SPAN_MARK_MARK); //Content text setting font color span.setSpan(textColorSpan, 0 , str.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //Return span return span; } int maxLines = mCollapsibleTextView.getMaxLines(); if (maxLines == this.maxLines) { //Set font size for content text span.setSpan(new RelativeSizeSpan(1f), 0, count - 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //Expand and hide setting font color //Set the font of the content text to be clickable and listen span.setSpan(new TextListener(), 0, count - 5, Spanned.SPAN_MARK_MARK); //Set the font color of the content text span.setSpan(textColorSpan, 0, count - 5, Spannable .SPAN_EXCLUSIVE_EXCLUSIVE); //Expand and hide setting font size span.setSpan(new RelativeSizeSpan(1.1f), count - 5, count, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //Expand and hide setting font can be clicked and listen span.setSpan(new ExpandAndHideListener(), count - 5, count, Spanned.SPAN_MARK_MARK); span.setSpan(expandAndHideColorSpan, count - 5, count, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } else { //Content text set font size span.setSpan(new RelativeSizeSpan(1f), 0, count, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //Content text setting font can be clicked and listen span.setSpan(new TextListener(), 0, count, Spanned.SPAN_MARK_MARK); //Content Text set font color span.setSpan(textColorSpan, 0, count, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // Expand and hide set font size span.setSpan(new RelativeSizeSpan(1.1f), count, count + 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); / / expand and hide set font clickable and listen span.setSpan(new ExpandAndHideListener(), count, count + 3, Spanned.SPAN_MARK_MARK); //Expand with hidden setting font color span.setSpan(expandAndHideColorSpan, count, count + 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } //Return span return span; } /** * 1. Expand and hide click events * 2. (Click events for content text and expand and hide text click events can be used all or only one here) * / private class ExpandAndHideListener extends ClickableSpan implements View.OnClickListener { //Text click event @Override public void onClick(@NonNull View widget) { //flag setting and re-request layout flagSetting(); } @Override public void updateDrawState(@NonNull TextPaint ds) { super.updateDrawState(ds); ds.setUnderlineText(false); } } /** * 1. Click event of content text * 2. (The click event of content text and the click event of expanding and hiding text can be used all or only one can be used here) */ private class TextListener extends ClickableSpan implements View.OnClickListener { //Text click event @Override public void onClick(@NonNull View widget) { //flag setting and re-request layout flagSetting(); } @Override public void updateDrawState(@NonNull TextPaint ds) { super.updateDrawState(ds); ds.setUnderlineText(false); } } / ** * Flag setting and re-request layout */ private void flagSetting() { flag = true; if (flag3) { //mCollapsibleTextView.getLineCount() <= defaultMaxLines returns true, otherwise false flag2 = mCollapsibleTextView.getLineCount() <= maxLines; //Re-request layout requestLayout(); } } /** * Layout setting * * @param changed * @param l * @param t * @param r * @param b */ @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l , t, r, b); //flag returns false if (!flag) { return; } //set to false flag = false; //flag2 is true expand if (flag2) { //expand state mCollapsibleTextView.setMaxLines(Integer.MAX_VALUE); //add "hidden" after the original string String allText = text + "hidden"; SpannableString clickableSpan = getClickableSpan(length, allText); mCollapsibleTextView.setText(clickableSpan); flag3 = true; } else { //flag2 is false to judge mCollapsibleTextView.getLineCount() > defaultMaxLines if (mCollapsibleTextView.getLineCount() > maxLines) { //close Starting state mCollapsibleTextView.setMaxLines(maxLines); //Get the position of the last character of the first line int lineEnd = mCollapsibleTextView.getLayout().getLineEnd(maxLines - 1); //Intercept the characters of the first line and replace the last five characters with "...Expand" String hiddenString = text.substring(0, lineEnd - 5) + "...expand"; SpannableString clickableSpan = getClickableSpan(lineEnd, hiddenString); mCollapsibleTextView.setText(clickableSpan); flag3 = true; } else { SpannableString clickableSpan = getClickableSpan(0, text ); mCollapsibleTextView.setText(clickableSpan); } } } }
2. The second step is to create a new layout file layout_collapsible_textview.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/mCollapsibleTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/transparent" android:text="内容" android:textSize="14sp" /> </LinearLayout>
3. The third step is to quote in the layout, com.tq.recorder.widget.customView.CollapsibleTextView is my package name, replace it with your own :
<com.tq.recorder.widget.customView.CollapsibleTextView android:id="@+id/mCollapsibleView" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="top" android:textSize="14sp" />
4. The fourth step is used in the code, both in the Activity and the adapter:
CollapsibleTextView view = findViewById(R.id.mCollapsibleView); //Incoming text content, default number of lines to display, (content text) text color, (expand and hide) text color view.setText("Incoming text content" , 1, Color.parseColor("#000000"), Color.parseColor("#3b76ec"));
----------------------------------------------------------------end---------------------------------------------------------