Reprint: HTTP://WWW.JIANSHU.COM/P/22FF8B5FDADC Build a new Android project, what would you do?
每个人对应用框架的理解不相同,但是最终达到的效果应该是一样:①降低项目的复杂性②易扩展、易修改、可重用性强、可维护性强③职责单一,功能清晰
In the Android development project, we first have to consider the common points of each project, such as: MVP, network request layer, base store view of base class, log log, App crash, refresh load more, Loading, ad map, support ListView, Recyclerview baseadater, notification bar immersion, picture loading cache, bottom navigation ...
So these are the features that every project has to need, so can you extract these generic things?
Project Engineering Building
APP Engineering structure: several common Android code architecture analysis
App project package structure. png
The role of the package name at a glance, when others take over the project will be relatively simple
- Adapter adapter, if the business complex, according to different business can add sub-package to classify
- API Network Request Layer encapsulation retrofit &OKHTTP3 &rxjava
- base class used to store the view, such as Baseacitivity, Basefragment, baseswipebackactivity, basepresent
- Manage is used to store some common management classes: Crash, Imageloader, Logger
- The model encapsulates the MVP logic pool, which is instantiated and BindView using the annotated contact and presenter, using Mpresenter directly when used
- UI Custom View swipe delete, bottom navigation
Common libraries Open Source Library selection
Project New Refactoring
Some of the common features of Android do some tidying up package into basic framework, easy to develop Android initial project quickly
Https://github.com/meikoz/Basic
Example of an imitation of the eyes demo: Basic+retrofit+okhttp+rxjava+glide
Please pay more attention to star and give support, from some of their practical experience to summarize this part of the common things as a good-natured share.
How Usagestep 1:
Setup ' Basic ' dependencies in Build.gradle file:
dependencies { ‘com.meikoz.basic:core:1.0.5‘}
Step 2:
Create subclass of application extends Mainapplication,initialize basic Buildconfig.debug is true in Super.oncreate () meth OD before for debug print log.
class BaseApp extends MainApplication { @Override public void onCreate() { RestApi.getInstance().deBug(true); super.onCreate(); }}
- Restapi.getinstance (). DeBug (True); To debug print logs, please delete this method before you go online # important #
Step 3:
App usage MVP pattern, View and Presenter need things.
View extends Baseview. Presenter extends Basepresenter<view>.
@Implement (Mainlogicimpl.class)PublicInterfaceMainlogici {voidOnloaddata2remote();InterfaceMainViewExtendsbaseview {void onloadsuccesshandler (String responce);}} public class mainlogicimpl extends BasePresenter <mainlogici. mainview> implements mainlogici { @Override public void onloaddata2remote () {GetView (). Onloadsuccesshandler ( "request succeeded, data returned")}}
How to initialize use the Activity.
ClassMainactivityExtendsActivityimplements mainlogici. MainView { @Override protected Class Getlogicclazz () {return Your interface mainlogici class; Mainlogic is the interface presenter to implement} @Override protected void Oninitdata2remote () {super.oninitdata2remote (); ((mainlogicimpl) mpresenter). Onloaddata2remote (); @Override public void Onloadsuccesshandler (string response) { //response is the data returned in presenter}}
- Super.oninitdata2remote (); Initializes the Presnter and BindView
Step 4:
Network:retrofit + okhttp
Generate different service based on different business
PublicClassApimanager {PublicStatic ApiserviceCreateapi () {return restapi.getinstance (). Create (Apiservice.class);} public static UserService createuserapi () {return Restapi.getinstance (). Create (Userservice.class); }}public interface apiservice {String base_url =" https://github.com/"; @GET ( "Api/v1/user") Call <Response> getuserinfo (@Query ( "UID") int uid)}
Cool Featurefeature 1: Realizing the Tab +fragment function
QQ Picture 20161121164914.png
PublicClassMainatyExtendsappcompatactivity {PrivateTabstripview Navigatetabbar;@Overrideprotected void OnCreate (Bundle savedinstancestate) {Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Navigatetabbar = (Tabstripview) Findviewbyid (R.id.navigatetabbar); Navigatetabbar.onrestoreinstancestate (savedinstancestate); Navigatetabbar.addtab (Homefragment.ClassNewTabstripview.Tabparam (R.drawable.ic_tab_strip_icon_feed,R.drawable.ic_tab_strip_icon_feed_selected,R.string.tab_bar_text_feed)); Navigatetabbar.addtab (Discoveryfragment.ClassNewTabstripview.Tabparam (R.drawable.ic_tab_strip_icon_category,R.drawable.ic_tab_strip_icon_category_selected,R.string.tab_bar_text_category)); Navigatetabbar.addtab (Autofragment.ClassNewTabstripview.tabparam (R.DRAWABLE.IC_TAB_STRIP_ICON_PGC, r.drawable.ic_tab_strip_icon_pgc_selected, R.string.tab_bar_ TEXT_PGC)); Navigatetabbar.addtab (profilefragment. class, new tabstripview. Tabparam (r.drawable.ic_tab_strip_icon_profile, R.drawable.ic_ tab_strip_icon_profile_selected, r.string.tab_bar_text_profile)); } @Override protected void Onsaveinstancestate (bundle outstate) {super.onsaveinstancestate (outstate); Navigatetabbar.onsaveinstancestate (outstate); }}
Feature 2: Achieve the effect of imitation iOS pop-up windows
The default cancel OK button. png
Setcancelable (True) displays the Cancel button by default, using the same syntax as native Alertdialog
new SweetAlertDialog.Builder(MainActivity.this) .setTitle("标题") .setMessage("描述详细内容?") .setCancelable(true) .setPositiveButton("确认", new SweetAlertDialog.OnDialogClickListener() { @Override public void onClick(Dialog dialog, int which) { Toast.makeText(MainActivity.this, "确定按钮", Toast.LENGTH_SHORT).show(); } }) .show();
There is only one OK button. png
! [Uploading QQ picture 20161121171109_005140.png ...]
Setcancelable (false) so that only one button is displayed
new SweetAlertDialog.Builder(MainActivity.this) .setTitle("标题") .setMessage("描述详细内容?") .setCancelable(false) .setPositiveButton("确认", new SweetAlertDialog.OnDialogClickListener() { @Override public void onClick(Dialog dialog, int which) { Toast.makeText(MainActivity.this, "确定按钮", Toast.LENGTH_SHORT).show(); } }) .show();
The left and right sides both set the button. png
- Left and right button customization
New Sweetalertdialog.builder (mainactivity.This). Settitle ("title"). Setmessage ("description details?") . setcancelable (False). Setnegativebutton ("Left",New Sweetalertdialog.ondialogclicklistener () {@Overridepublic void onclick (Dialog Dialog, int which) { Toast.maketext (Mainactivity. This, "left" + which, Toast.length_short). Show (); }}). Setpositivebutton ( "confirm", new Sweetalertdialog.ondialogclicklistener () { @Override < Span class= "Hljs-keyword" >public void onclick (Dialog Dialog, int which) {Toast.maketext (Mainactivity.this, " OK button ", Toast.length_short). Show (); }}). Show ();
Feature 3:commonadapter for ListView, Recycleradapter for Recyclerviewcommonadapter for ListView, Recycleradapter for Rec Yclerview
Implement void Convert (Viewholder helper, T item); Replace GetView ();
Feature 4:logger Beautiful log system
LOGGER.D (message);
LOGGER.I (message);
LOGGER.E (message);
LOGGER.V (message);
Logger.json (message);
Nice log. Pngto do something v2.0.0
- Add Loadingview before network request, add finish disappears
- Load failed unified failure page, support re-request
- Activity destroyed, disable network request function to avoid error
- Eyepetizerapp: Use the basic framework to complete your eyes app
Make your Android development easier