FoldTextView

Time:2021-6-1

A few days ago, because of business needs, I wrote aEllipsizeEndTextview , It happened that another similar method, foldtextview, was used in the project. Moreover, there was a bug in the text view display in the old library, so it was in place at one time, and it was rewritten according to the previous view.

warn

It’s not necessarily a good thing to write by yourself, because there are so many talented people in GitHub, and many libraries are widely used and have good compatibility. If you step on something you haven’t stepped on, you have to step on it again and again. It’s time-consuming to test it again and again. This foldtextview is not complicated. Another decisive reason is to meet the requirements, In many cases, some of the general view can not meet their own needs, this time do not force to engage in Sao operation, otherwise it is not worth the loss

Reasons for rewriting

Because of our own requirements, there is a set of special text matching logic, which needs to replace the text display. Therefore, there is a set of special encapsulation, which makes many third-party libraries unable to share and needs to modify the settext method. Therefore, it is also necessary for the third-party entrance to rewrite settext. Many libraries have complete functions, but they are not very compatible with their own businesses. In addition, there are many configurations, In addition, there was already an ellipsize end textview, so it is feasible to continue this general idea in a short time

imagine

As simple as possible design, easy to use, compatible with their own business, so it is directly in theellipsize_end textviewOn the basis of doing, the change is relatively small, rewrite the settext method, this is necessary

As you can see, it’s basically the sameEllipsizeEndTextview  The same processing, but because of its own business, there will be empty characters, so you need to deal with the empty characters

Problems arise

I went in to have a lookEllipsizeEndTextview  You should know that there is an additional processing, super.settext. Here we continue the previous dynamic measurement method, because we need to process the text displayed in the ellipsize end mode, and we need to leave more space at the end. At this time, the built-in method getcountline is not able to accurately calculate the interception, so we are sure that our own measurement is more flexible, but there is a problem, If there are characters such as characters in the text, the measurement is not accurate. It is clear that the line has been wrapped, but the width of the measurement result is the actual width of the character, and the width of the line cannot be measured. Therefore, there is no way at this time. We can only use the system’s layout.getcountline method

New solutions

If measurement doesn’t work, use it decisively   The layout. Getlinecount () method, which is known by its name, is used to obtain rows. In this respect, it is professional and does not need to followEllipsizeEndTextview  It is the same as leaving blank at the end, so using it has no effect. The general logic does not need to be changed. Just replace the method of measuring lines with the getlinecount

 

Here is the collapse effect, so you need to define the color and click event setspan at the collapse text

 

Here, as long as the text is set as the original textview, the folding effect will be triggered to facilitate the expansion of business requirements