Let textView be dynamically limited to one line in RecyclerView (ViewTreeObserver)


foreword

Keywords: OnPreDrawListener, ViewTreeObserver, correct removal, RecyclerView

1. Background

Common sense that everyone who is an Android player knows. Generally, one item of text in a list cannot be displayed. Generally speaking, a fixed number of lines is limited and an ellipsis is added behind it. Once a requirement was written happily, the result was UI acceptance. Tell me that this data is more important and needs to be displayed in full, but the data cannot exceed one line, which is more difficult. I said that there is no such function in the project, and I have never done it. Otherwise, give me a day to study For a moment, or else just take it easy, and then the UI asked their manager, who said, how can it not be done, it is impossible to take a day, today must be done, and feedback to my minister, at that time I the mood is like this
insert image description here

Two, solve

1. Use in the project

After asking colleagues for help, use the following code in the project to dynamically change the number of textView lines

    private void salaryTextObServer(TextView textView, float textSize) {
    
    
        textView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
    
    
            @Override
            public boolean onPreDraw() {
    
    
            //监听移除,不然就会嘿嘿嘿
                textView.getViewTreeObserver().removeOnPreDrawListener(this);
                int line = textView.getLineCount();
                if (line > 1) {
    
    
                    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize - 1);
                    salaryTextObServer(textView, textSize - 1);
                } else {
    
    
                }
                return false;
            }
        });
    }

Just pass in the textView that needs to change the number of rows dynamically, which is very convenient and simple.

2. Learn and understand

I don’t use the ViewTreeObserver class when drawing custom views or drawing custom views with a brush. In line with the idea of ​​learning and progress, I also searched for relevant information on the Internet.

a. What does ViewTreeObserver do?

As the name implies, it is an observation class that uses the view of the observer mode. It can be used when we need to dynamically observe the properties of the View's width and height. The following methods can be used under the ViewTreeObserver class, which are applicable to specific scenarios and used as appropriate

  • addOnWindowAttachListener window attach listener
  • addOnWindowFocusChangeListener window focus listener
  • addOnGlobalFocusChangeListener global focus listener
  • addOnGlobalLayoutListener global layout listener
  • addOnPreDrawListener pre-drawing listener (this is also used in this article, I understand it as before the view is drawn)
  • addOnDrawListener draw listener

a. What are the pitfalls of ViewTreeObserver?

1. The above listeners have a corresponding remove method. After adding the listener, you need to remove it in the method, otherwise an error will be reported (Why do you ask why an error is reported? It’s like asking you to keep looking at a beautiful woman, can you Persist for a whole day? After all, you have to take a break), and like the example in this article, if it is used in the list, add a specific view control to getViewTreeObserver(). (Why do you ask again? It’s like asking you to find an ugly duckling from a group of ducks. It’s always very laborious) 2. The
ViewTreeObserver obtained by View#getViewTreeObserver may not be the same instance every time
3. The use of ViewTreeObserver in RecyclerView


Summarize

Learning is like sailing against the current, if you don’t advance, you will retreat
insert image description here

Guess you like

Origin blog.csdn.net/shop_and_sleep/article/details/129086796