Chapter 1 reconstruction of app R & D, sleep notes, and ipd product R & D
1.1 re-plan the android project structure
You can re-plan the android directory structure in two steps:
1. Create the AndroidLib class library and transfer the business-independent logic to AndroidLib.
Acitivity stores Activity base classes unrelated to the business.
The cache package stores cache data and image-related processing.
The net package stores the underlying network encapsulation.
Ui stores Custom Controls
Utils stores various public methods unrelated to the business
2. Divide the categories in the main project into different categories and place them in various packages.
Activity is further divided by module, divides the activities of different modules into different packages. The adapter puts the adapter db to store SQLite-related logic engines to store business-related class entity. Therefore, the entity stores the interfaces interface. The interface name starts with I. listener is based on Listener. interface naming:
1. Each file has a separate class and should not be nested.
2. After Dividing acitivity, you can quickly locate the specific page. Developers are only responsible for their own packages, and the Development boundary is clear.
1.2 define a new life cycle for the Activity
There is a single principle: one class or method, only one thing.
Observe the onCreate Activity and find:
This Code contains three Logics:
1. initialize the variable initVariables
2. Load the layout, initialize the space, and attach the event Method to the control. InitView
3. Obtain loadData
You can rewrite the onCreate method in the BaseActivity class library AndroidLib.
Then inherit it to implement its three methods. 1.3 unified event Programming Model
According to the above btn_login.setOnClickListener (this); @ Override
Public void onClick (View v ){
Switch (v. getId ()){
Case R. id. btn_login:
// Perform the operation after clicking
Break;
}
} The R. id will be used each time, so it is highly respected: btn_login.setOnClickListener (this );
Btn_login.setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
// Perform the operation after clicking
}
}); 1.4 materialized Programming
FastJSON: https://github.com/alibaba/fastjsonGSON: https://github.com/google/gson fastJSON added after: 1: added the entity attribute of the symbol Annotation, use crash 2: There are generic attributes, if you use it, the system crashes and debugging will cause problems when there is nothing to package the signature.
-Keepattributes Signatue // avoid confusion over generics
-Keepattributes * Annotation * // non-obfuscation Annotation
Objects used for page Jump: 1. Json data of the call request 2. Uploaded from the previous page. 1.5 adapter template:
Public abstract class ListBaseAdapter <T> extends BaseAdapter {
Protected Context mContext;
Protected List <T> mValues;
Protected LayoutInflater mInflater;
Public ListBaseAdapter (Context context, List <T> values ){
MContext = context;
MInflater = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE );
MValues = values;
}
Public Context getContext (){
Return mContext;
}
@ Override
Public int getCount (){
If (mValues! = Null)
Return mValues. size ();
Return 0;
}
@ Override
Public T getItem (int position ){
If (position = getCount ()-1 | mValues = null ){
Return null;
}
Return mValues. get (position );
}
@ Override
Public long getItemId (int position ){
Return position;
}
@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
Return getItemView (convertView, position );
}
Protected abstract View getItemView (View convertView, int position );
Public void update (List <T> values ){
MValues = values;
NotifyDataSetInvalidated ();
NotifyDataSetChanged ();
}
} 1.6 secure conversion of Types
/**
* Convert a character to a number
* @ Param value
* @ Param defaultValue
* @ Return
*/
Public final static int convertToInt (String value, int defaultValue ){
If (StringUtils. isNullOrEmpty (value )){
Return defaultValue;
}
Try {
Return Integer. valueOf (value. toString ());
} Catch (Exception e ){
Try {
Return Double. valueOf (value. toString (). intValue ();
} Catch (Exception e1 ){
Return defaultValue;
}
}
}/**
* Determines whether it is null.
*
* @ Param text
* @ Return
*/
Public static boolean isNullOrEmpty (String text ){
If (text = null | "". equals (text. trim () | text. trim (). length () = 0
| "Null". equals (text. trim ())){
Return true;
} Else {
Return false;
}
}