preface
MTRVA from birth to now has been for some time, when the first version, I am very excited, this is my first time in the true sense, is also a historic progress, wrote this, remind themselves to stay up late to update version, just lying in bed thinking about where need optimization modification… Too many memories, so MTRVA as a RecyclerViewAdapter clean current, in the end what bird use?
The characteristics of
- Simple and fast to use, with most Adapters
- A single line of code refreshes a single level, which can correspond to multiple types, and refreshes with animation
- Supports add, delete, modify, and check operations
- Support asynchronous, high frequency refresh, extensible (such as with RxJava)
- A single level supports Loading, Empty, and Error page switching
- A single level supports header and footer
- Single level supports expansion and closing (closing minimum can be set)
- Supports Loading global Loading pages
- Support annotation generation class, reduce workload
- Refresh lifecycle callbacks are supported
- Compatible with low version RecyclerView
- Advanced usage, such as creating your own headerView and footerView, allows pages to switch between multiple pages
thought
This is the architecture diagram of MTRVA.
Imagine a common scenario (list page) where we first write an Adapter, then an XML for each of our types, and then we use a list in the Adapter to control the number of items, the entity classes that each item needs to render, and so on. If inserting logic into an existing list is complicated, even to the point where you need to control the type of start and end positions, you may write the logic quickly when you first write it. Fortunately, you don’t get an error. The sad part is that when you go back a few months later and you get it wrong, I’m sure you’ll be like me. “Oh my God.” Or add a type to a bunch of code that controls position. Sorry, I quit.
Resignation was joking, of course, at this time of MTRVA will play a role, in here, you don’t have to worry about data logic process (and the treatment should not be written in your business layer code), as long as you care about your UI logic, which is what you should care, also need to care about, also need to write, because our business is different, we are not the same.
MTRVA controls data processing. Is it a Helper class that works with Adapter, or is it a traditional Adapter architecture? Another bright spot of MTRVA is to use it together with Adapter. As long as your Adapter is RecyclerViewAdapter, it can basically be used together with MTRVA. If you can’t, please contact me. I will try not to interfere with your project Adapter.
There is a small incident here. Some students said that even if you only write UI logic, if there are enough types of UI logic, the code is still very complicated. This is not difficult. If you still don’t have a clear model in mind, refer to my demo. This is what I should do (manual antics), but don’t copy it.
In actual combat
I really do not want to write about the actual combat, because it is too simple, here is my article “BRVAH+MTRVA, complex? Does not exist” and the MTRVA documentation, you can not understand me, but this time I mainly want to write about the 2.0 update? What do I need to be aware of if my project has integrated MTRVA? I would like to write these more, at the beginning did not want to post an article, but I think some students may use a version, if it is not that version has not supported the requirements, is not to update the version, with good why do I update ah? When these students want to update, they may only have to look at the document, and the document only has the latest way of use, so I uphold the consistent style, every update I will write an article to explain the gap between versions, but this is a big version of the update, the noise is a little bit bigger.
Here’s a question that a lot of people ask about meshing and linear mixing. This actually does not belong to my category, but I will certainly carefully answer, the simplest is nesting, if a bit elegant can study the setSpanSizeLookup class GridLayoutManager method, you can refer to this article “RecyclerView: implementation of grid with header”.
update
Since it is a large version of the update, in the end what SAO operation?
background
Let’s start from the general direction. The core idea of this revision is to attach importance to and break through level from type thinking. No picture,say a J8! Let’s take a look at the layout model diagram for version 2.0 compared to version 1.0.
All right, let’s analyze briefly why this is a breakthrough. From version 1.0, it was clear that the outermost layer was defined as level, but the refresh level was type. When registering resources, a type corresponds to a level, which is hypocritical. What is the use of this level? If the two types are used at the same level, it is particularly inconvenient and has great limitations. Let’s look at version 2.0:
A level can register a variety of types, in the level of the type data order is unlimited, for example, the above type2 in the middle of the type1, obviously can ignore the order, let the type1 heap together, so that our level can control the UI more rich! Although this is possible prior to version 2.0, it is more demanding to operate.
This is a breakthrough in thinking. There are always changes to the API, and this shows the importance of architectural design. Do you have to find out one by one and then change a place, do you have to delete this and that, or just add classes… This is our language, and professionally, you need to keep in mind the six principles of Java design. I’m not flexible enough, but if you don’t use it, you’ll never use it.
I guess some of you are getting impatient. Can you get to the point? What’s changed? Dude, please put down your 40-meter machete, I’ll be right there.
API differences
Resources to register
First we started with registering resources, so we started like this:
registerMoudle(ItemEntity3.TYPE_3)
.level(2)
.layoutResId(R.layout.item_3)
.register();
Copy the code
And here it is:
registerMoudle(LEVEL_FIRST)
.type(TypeOneItem.TYPE_ONE)
.layoutResId(R.layout.item_first)
.type(TypeTwoItem.TYPE_TWO)
.layoutResId(R.layout.item_second)
.register();
Copy the code
As mentioned above, we can define multiple types, each of which corresponds to a layout XML. The original definition of level refers to the level of type order, while other types such as header, footer, loading, etc., remain unchanged.
The refresh method
Secondly, for a series of notify methods, all the original type parameters are changed to level, which also has a big impact. If you package the helper refresh method into adapter as I suggested, it will be so easy to change! See, this is the charm of design!
For example, these methods are commonly used:
public void notifyMoudleDataChanged(List<? extends T> data, int level);
public void notifyMoudleDataAndHeaderChanged(List<? extends T> data, T header, int level);
public void notifyMoudleDataAndHeaderAndFooterChanged(T header, List<? extends T> data, T footer, int level);
Copy the code
Resource adapter
The LoadingEntityAdapter change point is the same, the original callback was only type, this time add a parameter level, although one of the two can calculate the other. Here’s an example:
T createLoadingEntity(int type); ===>T createLoadingEntity(inttype, int level);
Copy the code
methods
- Change the getLevel method from private to public
- Remove the getCurrentRefreshType method and replace it with the getCurrentRefreshLevel method
- The global initialization loading class LoadingConfig and the corresponding Builder setLoading method parameter type changed to level
- Add getDataWithLevel method to get the corresponding data according to level
- The Type attribute of the HandleBase class is changed to Level as expected
annotations
I don’t know. Does anyone use this thing? Anyway, the previous version has been isolated, if you want to use another library, you can see the documentation.
The changes to the annotations are basically the same as above, mainly changing the original definition of type to level, using the same method.
gossip
Is that all you’re gonna change? Are we gonna make a splash, too? Sorry, that’s all. Sorry to bother you. You can even change type to level without thinking, but… However, the changes to my underlying logic were terrible. I can’t remember how many changes I made. I just remember frantically testing for fear that any changes would affect the use of students who had upgraded version 2.0. Of course, students who have integrated MTRVA do not have to update, this is an improvement in the architecture, convenient for future expansion. If you like the new version like me, you can try to update the new version. Here are some common ways to update the new version: First download the official demo, crazy play, play demo is not satisfied with their own code to play, play almost the same time integrated into the project, crazy self-test, no problem with the version of the test together with our lovely testers. If there is a better way to contact me, I will tell you after the correction, we can study together.
Careful friends found that our space is not very long, the protagonist of this time is MTRVA, is a library, but for me it is a kind of feelings. Let me first talk about my criteria for choosing a library. First of all, what are the characteristics of the library? How will it benefit my current project? Do I have a similar library in my current project? How does it compare? If I choose to use it, whether it is simple to use, whether the integration is complex, whether it will have a great impact on my project, whether the author will maintain it later, and so on. This is their own ideas, I hope to help you, welcome to add.
Originally this month the topic of the article is Android performance optimization, before the company is a small company (although now is still a small company), belongs to the needs to complete everything, but as an excellent programmer, the application requirements of their development only meet the needs, that is too low, sooner or later will be eliminated. Next month I may release a little understanding of performance optimization, hope to help you, wait and see! By the way, my CrazyDaily project has also been updated. In addition to the MTRVA update, it has also replaced content jokes with funny videos, and you know why!
I want to emphasize that my open source library is something THAT I will keep updating until no one else uses it or I’m no longer in the industry. Although there are times when multiple users ask several questions at the same time, some of them are still difficult questions, and I have thought about deleting libraries or stopping maintenance. But think of others with my library is a kind of recognition to me, is highly of me, at the same time to meet their own insignificant vanity, I instantly come to strength, ha ha, do not know this is not self hypnosis. In the future, the open source library may be less written, but the spirit of open source must be there. I will integrate some of the more powerful and practical writing methods into my project CrazyDaily. If you like, you can pay attention to a wave of star, reveal the story wave, and there will be a large wave of custom controls and animation flooding into the project.
The most important point is that you can find my contact information in my blog, add casually, but not necessarily instant reply, please understand. Don’t call me a big guy (manual face covering) in the first sentence of your friend. I’m just an Android developer scum. Several students asked me why I didn’t form a group? I’m sure you must have added a bunch of groups, there is no use, you have no 13 number in mind? If you have any questions, just ask! Maybe I have never met your problem, but I will try my best to solve it for you. We will study together. Why not? Of course, if you can search, don’t come!
Finally, thanks to those who have supported me all the time!
portal
Github:github.com/crazysunj/M…
Documents: crazysunj.com/2017/08/14/…