Preface To be honest, I haven't updated my blog for a long time. The main reason is that the framework issues I have been maintaining have been endless, and answering questions in the technical exchange group has also exhausted me physically and mentally. So I would like to say sorry to those who follow me here for not bringing you benefits regularly. So here is a big benefit for everyone: Ai Ya Mei Paper - a complete App of Retrofit & RxJava & MVP & Butterknife. Speaking of the answers to the questions that have exhausted me recently, it is undoubtedly what has made me go further and further on the road of open source. Although I am not a technical expert, I am still bombarded with some very simple questions. In fact, the author really refuses to answer them in his heart. I have to say, I write to you and the group owner in the technical group, why do you ask questions, but no one always replies to you! It is well written. Overview Without further ado, I believe most people can say something about MVP (Model View Presenter), "an evolved version of MVC", "completely decoupling Model and View", etc., but those who have used MVP will definitely feel that in Android, the code is very clear, but there are too many classes. For most people, when watching the MVP Demo, it is nice at first glance, but when they are asked to write an example, they have a headache and can't write it. But it is true that the MVC model is more like a smooth sailing. Just put your business logic into the Activity and it will be done successfully. I have to say that we did use MVC to write our previous projects. It is easy to find that any Activity code is hundreds or thousands of lines, and even more than 10,000 lines. It does look like that, but if you think about it carefully, there is actually very little that the View can do for the layout file. In fact, the data binding operations and event handling operations in the layout file are all in the Activity, making the Activity look like both a View and a Controller. Apart from the ugly appearance of the code, it is really hard to read the code later. Don't believe it? Take a look. Maybe MVC is OK for functions with simple business logic, but have you ever thought about what to do if your product requirements change later? Yes, you accept the rape of product requirements, but you still have to endure the humiliation. With increasingly complex business logic, your Activity and Fragment codes are increasing, which eventually leads to code explosion and difficulty in maintenance. After browsing the Internet, I found that there are many articles about MVP, which shows the popularity of MVP. However, most of the articles only talk about theory, and the better ones will come with a simple login demo. However, a simple demo is difficult for people who are new to the MVP mode to master its use. So AiAMeiPie came into being. What is MVP Of course, we cannot go off topic. We have given a brief overview of MVP above, so let’s use a simple diagram to illustrate it below. As shown in the figure above, in the project, View and Model do not interact directly, but use Presenter as a bridge between View and Model. The Presenter holds references to the Interface of both the View layer and the Model layer, and the View layer holds references to the Interface of the Presenter layer. When a page of the View layer needs to display certain data, it will first call a certain interface of the Presenter layer, and then the Presenter layer will call the Model layer to request data. When the Model layer data is loaded successfully, it will call the callback method of the Presenter layer to notify the Presenter layer that the data loading is complete. Finally, the Presenter layer calls the View layer interface to display the loaded data to the user. This is the core process of the MVP mode. The advantage of this layering is that it greatly reduces the coupling between the Model and View layers. On the one hand, the View layer and the Model layer can be developed and tested separately without relying on each other. On the other hand, the Model layer can be encapsulated and reused, which can greatly reduce the amount of code. Of course, MVP has some other advantages, which will not be repeated here. Function Display Here I will just show you the functions of the useful information section. The layout is fairly simple.
The dry goods module is a Fragment, which contains a RecyclerView that supports pull-down refresh and pull-up to load data. So our Presenter and View only need to define simple methods. 1) Display the loading progress bar during the data loading process; 2) After the data is loaded successfully, the Adapter is reminded to refresh the data; 3) Loading failure window reminds users of relevant information; 4) Hide the progress bar when loading is finished;
Write the Presenter implementation class.
Write an Adapter to display data.
***Of course it’s Fragment.
Project screenshots Let me show you the project screenshots to prevent you from getting nervous. Conclusion Aiyameizhi is a practical app that integrates mainstream frameworks such as MVP, Retrofit, and RxJava. The project resources come from the practical concentration camp of coders. The amount of code is not much, but it basically covers all aspects. The interface adopts the design style, so it is also a good medicine for learning design. The author also hopes to continue to go further and further on the road of open source, and please support it. |
<<: Android imitates Huawei's weather drawing dial
>>: Use Node.js to segment text content and extract keywords
Google released Android P, which focuses on intel...
As the largest knowledge sharing platform in Chin...
A few days ago, a screenshot of a list of mobile ...
Since the outbreak of the epidemic began in early...
Before the launch of Juleliang Qianchuan, the Dou...
[[244806]] With the iPhone XS series going on sal...
I think everyone knows that the placement of the ...
Internet jobs are usually divided into three cate...
According to the latest research of the "Chi...
Now we often hear that taking children to outdoor...
"Apply the most expensive facial mask and st...
Looking back at 2015, "Internet Plus", ...
In recent days, a piece of news about China's...
There are two types of Daqing Commerce WeChat Min...
This article was reviewed by Shao Lin, a popular ...