Practicing the App developed by MVP architecture is a summary of what I have learned. It has been done for some time, but the interface is quite large, and there is a lot of code. A lot of encapsulation has been done inside, and the overall code is very clean. Keep the naming of files (Java, XML, resource files) and the layout simple. The code will not write very complex, the whole code structure has a high degree of unity, the structure is relatively simple and clear, easy to understand. There is a lot of encapsulation, including base class construction and tool class encapsulation, combined with the use of Dagger2 can greatly reduce the V layer (Activity and Fragment code), if you look at the source code you will find that most of the V layer valid code is not more than 100 lines (excluding import statements). Almost every interface uses knowledge deliberately, so some of the features are not necessary but simply use knowledge.

CSDN:blog.csdn.net/github_3518…

Project address: github.com/Rukey7/MvpA…

Screenshot















The project structure

Let’s talk about the general structure of the project, so it’s easier to look at the code. Of course, everyone has their own style of writing code, not necessarily someone else’s style is right for you. But I think it’s important, it’s kind of the foundation of the whole App. You can skip this if you don’t want to.

Project directory structure:





Above is the general directory structure, a more detailed structure see the source code, here only the MVP related parts. The V layer and P layer implementations of the same interface are put together in the same file, and the related interfaces are put together, as is the Google example code, if not the base interface. The advantage is the time to look at the code can be easily access, Java is the programming to an interface is known to all, and I in the code V holding P layer of implementing an interface, the concrete implementation class P layer is through the Dagger into the same P layer have V the implementation of the interface, specific V layer is through the Dagger into the implementation class. Imagine if the implementation classes of layer V and layer P are open, when you press F3 to jump to the interface of layer P, and then find the corresponding implementation class in the whole project, if the project structure is messy, it will be more difficult. I have extracted all the common interfaces and put them in the Base folder. This will reduce file creation somewhat. Using MVP architecture will generate many more files than usual, especially with Dagger.

Knowledge summary

Here list a few knowledge points that I think may have reference value more, be regarded as a summary of the use of some knowledge points.

1. The construction of base classes in V layer, including BaseActivity and BaseFragment, provides the same interface externally, so that the code is highly consistent when writing activities or fragments. At the same time, the unified injection of P layer implementation and a network loading and exception control. BaseFragment does lazy loading processing, as long as inherit the base class can achieve lazy loading;

SplashActivity (SplashActivity, SplashActivity, SplashActivity, SplashActivity, SplashActivity, SplashActivity, SplashActivity, SplashActivity, SplashActivity, SplashActivity)

3, the home page of HomeActivity can see the processing of DrawerLayout with Fragment switch;

4, news homepage NewsMainFragment can see the ViewPagerAdapter in the process of adding and deleting and position exchange, as well as the use of RxBus, need to see with the column management interface;

5, news list NewsListFragment of a variety of different layout processing, the head to add unlimited round cast Banner, list add into the animation effect, list click ripple effect, load more, refresh, load and network exceptions and so on;

NewsArticleActivity with ScrollView to do the header animation effect processing, custom ScrollView can be implemented when the article scroll to the bottom can continue to pull up to load the next article, the use of prompt dialog box, the use of ViewStub, You can swipe left to exit by inheriting BaseSwipeBackActivity. In the face of RichText processing with RichText this library, will report memory leaks need to pay attention to;

7. You can implement a list with a header in SpecialActivity, you can click an index to go to the corresponding position on the list, you can customize the TagLayout for the flow of tags, and you can customize the CoordinatorLayout behavior.

8. PhotoSetActivity loading and handling of loading exceptions when ViewPager displays pictures, and custom DragSlopLayout layout;

9, column management in ChannelActivity RecyclerView drag processing and two list associated processing, RxBus send events, database use;

10. Use of property animation Keyframe and PropertyValuesHolder in PhotoMainFragment;

Use of RecyclerView waterfall flow in WelfareListFragment. The reason why I write two same interface is because the data returned by netease picture interface has resolution parameters. You can directly set the size of the picture zoom display to achieve a real waterfall flow, absolutely not like some App waterfall flow picture truncated situation. However, the picture data returned by the dry goods network interface does not have the parameter of resolution. By calculating the resolution of each picture before displaying it, the speed will be slower than the previous one, and the specific experience can be seen in the App effect.

12, big picture BigPhotoActivity picture full screen display, occupy SystemBar and NavigationBar, picture collection, Glide picture download, handle PhotoView and ViewPager use of the exception;

13. Video player made by IjkPlayer in VideoPlayerActivity, video barrage processing, video download, favorites and sharing;

14. Use startActivityForResult in related interface of picture and video collection, click feedback of list, delete animation of list;

Package and use of FileDownloader, download control, progress display, delete operation processing;

16, set the interface about the simple use of PreferenceScreen, this thing should not be used much, the usage is quite special;

17, each interface switch show different enter exit animation, as far as possible using the overridePendingTransition () to implement different animation effects, 5.0 has actually experience better interactive animation processing interface, but this adapter to 4.0;

18. Retrofit is used with RxJava, OkHttp interceptor for caching, Dagger2, RxBus encapsulation, and list library encapsulation.

19, simple multi-task download package, pay attention to the inside of the use of the download without multithreading, also did not use Service, before also have done multithreading download behind change, there is a good open source project FileDownloader, interested in their own look;

20. Newly added Tinker hotfix architecture, in fact, the examples of Tinker are organized in a Module for easy reference and usage addition.

Third-party libraries

List the third-party libraries used:

Leakcanary: Detect application memory leaks

Butterknife: dependency injection for Android controls and callbacks, the masterwork of JakeWharton

Dagger2: Dependency injection library for Android and Java

Rxjava: a library that implements asynchronous operations is very popular

RxAndroid: Rxjava binding library for Android

RxBinding: Works with Rxjava to handle asynchronous calls to controls

RxLifecycle: Prevents memory leaks caused by Subscription in RxJava

RxPermissions: Developed based on RxJava to help handle runtime permission checks in Android 6.0

Retrofit: You’ve probably used all of them

Okhttp: OkHTTP and Retrofit are a perfect match for networking

GreenDAO: ORM database, can be used with RXJava

Logger: Log library, makes the printed Log very nice

Glide: Google released images load library, there is a very good guide here: https://mrfu.me/2016/02/27/Glide_Getting_Started/

BaseRecyclerViewAdapterHelper: useful RecyclerView multi-function adapter libraries, project and I was not directly use the library, but according to my own habits in it some changes have been made on the earlier code

Recyclerview -animators: Recyclerview animation library, built in a lot of animation effects

CircleImageView: a very common library for displaying circular heads

PhotoView: A library of images that can be zoomed in and out based on gestures. This is also common

AndroidImageSlider – a library to display header banners. Lots of animations, but depends on Picasso and nineoldandroids libraries

NumberProgressBar: Sexy numeric progress bar

FlycoTabLayout: A Tab library with more styles than TabLayout

FlycoDialog: Multifunctional Dialog

FlycoLabelView: Add corner marker library

Gson: Helps with Json and Object conversion, which is also commonly used

Fastjson: the same function as Gson, some places Gson can not handle you can try this, Alibaba produced

Ijkplayer: a video decoding library produced by station B

DanmakuFlameMaster: also from station B

ShineButton: hit buttons with cool effects, mainly used to display animations like favorites

RichText: RichText processing library, very convenient to use but with memory leaks

Android-spinkit: Drawable that integrates various animation effects. The code is well packaged, and animation is not only used in View

Filepicker: This is a file selection library that handles PreferenceScreen. PreferenceScreen doesn’t seem to be used very often

Tinker: wechat Android hot patch solution, powerful, compared to other hot patches see here wiki

DragSlopLayout: a library to assist in developing drag and drop functions, which I packaged to do some of the App’s functions – and now use on work projects

IjkPlayerView: based on the development of ijkPlayer, but also to do the App’s video playback function package library – -, which added a bullet screen function, interested can see

TagLayout: this library can be used as a separate button for special click effects. Now I use this library to handle all the buttons that need to be clicked

other

The first time to write an article in Jane, found much easier to use than CSDN. The project will be updated continuously. If you have any new ideas, you can send me a message if you have any questions. I hope it will be helpful to you.