编码规范 - wongainia/android- GitHub Wiki
学习android一定要注意规范,没有规则不成方圆,在android中activity的1. _耦合_很严重,一定要规范 class import android.content.Intent; import android.content.res.Configuration; import android.content.res.Resources; import android.os.Build; import android.os.Bundle; import android.support.annotation.CallSuper; import android.support.annotation.IdRes; import android.support.annotation.LayoutRes; import android.support.annotation.NonNull; import android.support.annotation.Nullable; /** *组织结构顺序 *1.static final属性 *2.对象属性 *3.constructor *4.生命周期以及其他回调方法 *5.public 方法 *6.private / protected 方法 */
资源文件 命名规则
- Activity 布局文件: activity_xxx.xml
- Fragment 布局文件: fragment_xxx.xml
- Dialog 布局文件: dialog_xxx.xml
- Drawable 中自定义图形: shape_xxx.xml 属性顺序 1 控件ID 2 控件位置大小信息 3 其他信息
参数的检查
/**
-
写在函数开头, 如果不符合条件则 return */ public void foo(String arg) { if (TextUtils.isEmpty(arg)) { return; }
... }
/**
- 多个条件优先级不明确时, 使用
()
保障优先级正确 - 每行判断条件最好是一个逻辑上的分组
- 把
&&
/||
等连接符写在换行前面, 强调逻辑关系 */ if (cond1 && (cond2 || cond3) && (cond4 && (cond5 || cond6))) { ... }
/*
- 对于区间比较, 建议以下格式 (min <= value && value < max). 这样便于理解和调试
- 此外, 对区间采用左开右闭条件: [min, max) */ if (123456 <= value && value < 3456789) { ... }
/**
- 对于条件分支比较多的情况, 可以使用以下 'if false' 开头的方式, 便于结构的统一 */ if (false) {
} else if (0 <= value && value < 100) { ... } else if (100 <= value && value < 200) { ... } else if (200 <= value && value < 300) { ... } else { ... }
/**
-
switch
语句中, 每个 case 最好都 break/return, 并且建议同一个 switch 要么都 break, 要么都 return, 不要交叉使用 -
如果某个 case 需要 '穿越', 请加注释 */ switch (value) { case 0: break; case 2: return;
default: break; } 打开 cursor 的正确姿势
Cursor cursor = ...; if (cursor.moveToFirst()) { do { ... } while (cursor.moveToNext()); } cursor.close();
要
使用 RecyclerView 代替 ListView 使用 NestedScrollingParent/NestedScrollingChild 处理滑动冲突 不要
不要使用 AsyncTask, 使用 RxJava 中相关的方法代替