將現有框架封裝進module層
新建一個空項目
引入框架
在主module(app)的build.gradle中引入開源框架
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
//註解框架需要插件
def AAVersion = '3.2'
apt "org.androidannotations:androidannotations:$AAVersion"
compile "org.androidannotations:androidannotations-api:$AAVersion"
//ok網絡框架
compile files('libs/okhttp-2.5.0.jar')
//同上
compile files('libs/okio-1.6.0.jar')
//gson
compile 'com.google.code.gson:gson:2.2.4'
//圖片框架
compile 'com.github.bumptech.glide:glide:3.4.+'
//下拉列表框架
compile 'com.android.support:recyclerview-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
compile 'com.android.support:support-v4:23.3.0'
//autolayout
compile 'com.zhy:autolayout:1.4.3'
}
創建項目結構
1. 適配器包
2. 自定義view
3. dialogFragment封裝包
3. okhttp框架封裝包
4. 工具包
每個包分別導入代碼
導入適配器代碼
由於不涉及資源文件,注意把import中的包名個別修改就好
導入自定義view
目前只加入一個每個項目中通用的圓形圖標控件CircleImageView,需要在資源包的attrs.xml中添加代碼
<declare-styleable name="CircleImageView">
<attr name="civ_border_width" format="dimension" />
<attr name="civ_border_color" format="color" />
<attr name="civ_border_overlay" format="boolean" />
<attr name="civ_fill_color" format="color" />
</declare-styleable>
然後修改CircleImgeView的R調用
導入基本工具包
1. log管理類
2. 網絡管理類
3. meta-data數據類
4. Toast管理類
5. 文字管理類
6. UI相關類
基本工具包直接複製粘貼,無耦合
後面考慮如何降低耦合加入版本升級和頭像上傳工具類
導入dialog
1.導入代碼
1. DialogFragment抽象類
2. Dialog工廠類
3. 確認對話框
4. 加載對話框
2.在資源包中的styles.xml中添加
<!-- 自定義彈出對話框 -->
<style name="dialog" parent="android:style/Theme.Dialog">
<item name="android:background">@android:color/transparent</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
</style>
3.在資源包中的layout中添加custom_dialog.xml和custom_loading_paogressbar.xml文件以及文件相關的圖片顏色資源
在這裏順便把colors.xml裏面的顏色值都扔上去
導入okhttp封裝代碼
有大量import代碼需要修改
導入BaseActivity和BaseFragment
類裏面只保留最基本的結構
項目基本框架完成
還需要在build中把當前module該爲library狀態
打開appmodule的build.grade
apply plugin: 'com.android.application'
改爲
apply plugin: 'com.android.library'
並去掉defaultConfig中的applicationId
後續操作
1. 項目實踐應用
2. 添加常用第三方框架,例如極光推送、百度統計、高德地圖
3. 網絡自有封裝包考慮官方retrofit