Preface Today we will talk about findViewById, which is commonly used in projects. This thing can be simply understood as: initializing controls, instantiating controls, and facilitating other operations. Generally speaking, we usually write like this:
The example above is very simple, just initialize a TextView, but in actual projects, each Activity, Fragment or Adapter has n controls, each of which requires us to instantiate the control before we can operate it. FindViewById again and again is so annoying~! Is there a good way? Of course there are many ways, but we have to find the one that suits our project. The following will give you examples one by one~ Simplify findViewById by annotation In the past few years, Xutils has been very popular. There are many reasons for its popularity. I am more optimistic about the ease of use of Xutils. At least it encapsulates many commonly used tools for us, such as the commonly used disgusting image processing, which Xutils has good support for. Similarly, Xutils also supports annotations to simplify findViewById. A simple example is as follows:
The more famous ButterKnife, I also studied it before, the relevant article address is as follows: 《Play with ButterKnife in one article to make the code more concise》 A simple example is as follows:
The above are just two examples, at least the ones I have used. Of course, there are many other useful methods that support annotations. Welcome to communicate and learn together~ Personal encapsulation findViewById I was searching online just now and suddenly saw that a friend had packaged one himself after being inspired by his teacher. I thought it was good and decided to try it out to see if it works. After a simple modification, it passed the test and felt pretty good. Here is the source code for everyone:
So how do we use it? Simply put, there are two ways to call:
Another point is that you can expand the encapsulation class according to the project. Here are two specific ways to call and run the results: Method 1 calling method:
Running results: Method 2 calling method:
Running results: Simplifying findViewById with generics Generally speaking, we will define a generic method to obtain View instances in BaseActivity, as follows:
The call is very simple, as follows:
This result is needless to say, right? Extract generic methods to public classes This is the same as the previous method, but the previous method may have some flaws. What should I do if I call it in a non-Activtiy? Do I have to copy it once in each Base? I personally don't think it is very satisfactory.
In this way, the call is a bit disgusting, as follows: The calling method in Activitiy is as follows:
The call in Fragment is as follows:
If you like to copy back and forth, this may be a solution. Google's DataBinding I just learned about this a few days ago and never paid attention to it before. But after a brief understanding, I found that it is really 666. I guess the reflection or annotation method has very 666 performance. The following is a comparison of the current 6 statements: DataBinding completely surpasses findViewById! findViewById is essentially a traversal search of the view tree. Each time a control is called, it will be searched once. Although it has O(n) performance, multiple calls will become O(n)xm. But DataBinding is different. It finds all controls through a traversal and assigns them to the corresponding variables. It does not rely on findViewById at all. In any case, the complexity is O(n). The same is true for code generation, but the data binding framework provides more functions, improves work efficiency, and writes safer and more reliable code. So, what reason do we have to refuse? Just understand it briefly, otherwise you will be confused. Introduction to DataBinding DataBinding is a framework introduced at the IO Conference in 2015. Its literal meaning is data binding. In general development, an Activity needs to implement both network requests and interface rendering/user interaction. If the functions of a page are more complex, it will be more difficult to maintain the project later. Therefore, the introduction of this framework is conducive to simplifying functional modules and trying to separate the interface rendering/user interaction functions into separate modules. Feel the charm of DataBinding Nothing is more practical, convenient and quick than coding directly. Let's start DataBinding Study through a simple example. 1.Configure DataBinding in build
2. Write our layout file. Here we should remember the following points:
Based on the above two points, write and implement our layout file:
After the layout file is ok, we will implement our Activity. How to call it? Look at you~ 3. Call in Activity Build the project and then call it as follows:
Regarding the above content, we can also write it like this. Suppose, we want to display the student's name, then we will proceed in three steps. Step by step: define a simple entity class
Step two, modify the layout file:
Here is a brief explanation for everyone:
Three steps, how to play Activity?
Look at the running results. Chairman Mao once said: Practice is the only criterion for testing truth. The above are two simple examples of implementation methods. You need to get other usage skills yourself. Here is the official API address: https://developer.android.google.cn/reference/android/databinding/package-summary.html Here, I will explain the above Activity calling content to you. After building, a file with layout name + Binding will be generated for us. Our main operation is to be implemented through it. Let’s see how to operate in the related calling class. First of all, you should know that DataBinding is a framework that implements data and UI binding based on the MVVM concept. So what is MVVM? Here is a brief understanding: MVVM is the abbreviation of Model-View-ViewModel, which can also be simply understood as an upgrade of MVC, but I have never used it, so I don’t know what advantages it has. I guess it is also about decoupling and scalability. Based on the previously mentioned DataBinding, all controls are found through a traversal and then assigned to the corresponding variables. Let's take a look at how DataBindingUtil operates. 1. Clicking in, we can clearly see that it calls a generic setContentView method, which is simply translated.
2. Continue reading, by setting the layout, get the View instance and find the content to be bound, and then return it through the bindToAddedViews method.
3. Finally, we will find that he binds and adds View returns by checking the number of nodes
4. I wonder if you have noticed that DataBindingUtil has a static constant:
Judging from the name, it should be a Map type with key value. There is no doubt about it. Click to continue taking a look.
5. It’s a mess, but have you noticed anything here?
What a familiar content! Isn't this our layout? By determining which layout it is, we can bind the corresponding source. 6. Continue to click in, we will find that it will first go to the view to verify whether the tag of the current layout has been added. If it has been added, it will directly return to the ActivityMainBinding class generated for us.
7. Check the ActivityMainBinding construction method generated for us
This is a simple analysis. If there are any errors, please point them out. Let's make progress together. Conclusion get skills apply skills find problems solve problems record problems The road to becoming a god is full of holes, but it is not enough to fill them up Come on everyone |
<<: Building a simple logistic regression model from scratch using TensorFlow
According to foreign media reports, virtual reali...
According to the official website of the Nobel Pr...
Probably starting from the end of 2018, it can be...
Since entering 2017, rumors about the iPhone 8 ha...
The most profitable industry nowadays is the &quo...
In the field of condensed matter physics, the res...
[51CTO.com Quick Translation] We have compiled va...
This era does not only belong to young people. In...
1. Introduction: Nature’s mysterious “lazy” law H...
Previously, the resignation of LeTV Auto co-found...
In order to optimize the quality of keywords, you...
What is the price for developing Datong Jewelry M...
There are many kinds of pet dogs nowadays, with s...
If you want to shoot short videos, choosing a goo...
If your blood pressure is high, eat less salt - t...