Writing in the front
Text is too long to expand or collapse. My original idea was to use the getLineCount() method of The TextView to determine the number of lines of text content and expand or collapse this way. In fact, by calling this method, getLineCount always gets zero rows when the view is not fully drawn. This problem can be solved by using the observer to listen. The following is the specific operation.
Sets the maximum number of rows to display
Set the maximum number of lines to 5 as required by PM:
<! <TextView Android :layout_width="match_parent" Android :layout_height="wrap_content" android:gravity="start|center_vertical" android:minHeight="30dp" android:textColor="@color/menuFont" android:textSize="16sp" android:maxLines="5" android:ellipsize="end" /> <! <TextView android:layout_width="match_parent" Android :layout_height="wrap_content" android:gravity="start|center_vertical" android:text="@string/pack_down" android:textColor="@color/colorPrimaryDark" android:textSize="16sp" android:visibility="gone" />Copy the code
Android :maxLines= “5” Android: Ellipsize = “end”
Create observer
Create an observer in the Adapter for the TextView that is responsible for presenting the content
/ / create the observer, dynamicContent is responsible for the content show the TextView ViewTreeObserver ViewTreeObserver = holder. DynamicContent. GetViewTreeObserver ();Copy the code
The listener is then used by the observer, which needs to be removed to avoid repeated calls by the observer listener
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void OnGlobalLayout () {/ / avoid the Observer to monitor repeated calls, remove the ViewTreeObserver Observer = holder, dynamicContent. GetViewTreeObserver (); observer.removeOnGlobalLayoutListener(this); // Write the folded or expanded code you need here. }});Copy the code
I then use the getLineCount method in the listener above to determine if I need to expand or collapse the action
if (holder.dynamicContent.getLineCount() >= 5) { holder.packTV.setVisibility(View.VISIBLE); Pack up / / full/holder. PackTV. SetOnClickListener (new View. An OnClickListener () {@ Override public void onClick (View View) {if (! Holder.packtv.settext (r.string.pack_up); isPack = true; holder.dynamicContent.setMaxLines(50); } else {// The text button expands (contents are folded) holder.packtv.settext (r.string.pack_down); isPack = false; holder.dynamicContent.setMaxLines(5); }}}); } else { holder.packTV.setVisibility(View.GONE); }Copy the code
The maximum number of expanded rows I set above is 50. This allows you to expand or collapse through observer listening. And isPack is used to mark whether the current state is expanded or folded.
Private Boolean isPack = false;Copy the code
Paste the complete code block
If the View is not fully drawn, the text length of all TextViews is 0. Used to monitor can solve ViewTreeObserver observer ViewTreeObserver = holder. DynamicContent. GetViewTreeObserver (); viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void OnGlobalLayout () {/ / avoid the Observer to monitor repeated calls, remove the ViewTreeObserver Observer = holder, dynamicContent. GetViewTreeObserver (); observer.removeOnGlobalLayoutListener(this); if (holder.dynamicContent.getLineCount() >= 5) { holder.packTV.setVisibility(View.VISIBLE); Pack up / / full/holder. PackTV. SetOnClickListener (new View. An OnClickListener () {@ Override public void onClick (View View) {if (! Holder.packtv.settext (r.string.pack_up); isPack = true; holder.dynamicContent.setMaxLines(50); } else {// The text button expands (contents are folded) holder.packtv.settext (r.string.pack_down); isPack = false; holder.dynamicContent.setMaxLines(5); }}}); } else { holder.packTV.setVisibility(View.GONE); }}});Copy the code
Write in the last
The project is about to end, record the problems encountered in the process, about the function of expanding and folding the fake circle of friends, I did it a long time ago, I forgot which big god’s blog I referred to at that time, I referred to some blogs and articles on the Internet, there may be similar places, sorry.