越來越多的大公司都采用了Kotlin做為開發(fā)android的第一語言,為了跟上時代和響應谷歌的號召葵蒂,今天給大家介紹一個我自己編寫的Kotlin+Retrofit+Rxjava+MVP的框架苗踪。詳細代碼已經(jīng)在github上MCLibrary
框架的整體架構
大概說一下框架的文件目錄鲸睛。
先介紹一下build.gradle
//Diaog樣式
compile 'com.github.ybq:Android-SpinKit:1.1.0'
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
//網(wǎng)絡請求 retrofit+okhttp+gson
compile 'com.squareup.retrofit2:retrofit:2.2.0'
compile 'com.squareup.retrofit2:converter-gson:2.2.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.2.0'
compile 'com.squareup.okhttp3:okhttp:3.8.0'
compile 'com.squareup.okio:okio:1.11.0'
compile 'com.github.bumptech.glide:okhttp3-integration:1.4.0@aar'
compile 'com.squareup.okhttp3:logging-interceptor:3.8.0'
compile 'io.reactivex:rxjava:1.1.0'
compile 'io.reactivex:rxandroid:1.1.0'
//圖片加載
compile 'com.github.bumptech.glide:glide:3.7.0'
//design
compile 'com.android.support:design:26.0.0-alpha1'
//二維碼
compile 'com.google.zxing:core:3.2.1'
//Json數(shù)據(jù)解析
compile 'com.google.code.gson:gson:2.8.0'
//適配html代碼
compile 'org.jsoup:jsoup:1.10.1'
//圖片選擇器
compile 'com.lzy.widget:imagepicker:0.5.5'
//事件分發(fā)
compile 'org.greenrobot:eventbus:3.0.0'
//Banner
compile 'com.youth.banner:banner:1.4.8'
//Xrecycler列表
compile 'com.jcodecraeer:xrecyclerview:1.3.2'
//圓形頭像
compile 'de.hdodenhof:circleimageview:2.1.0'
//城市昨寞、時間等選擇器
compile 'com.contrarywind:Android-PickerView:3.2.5'
//滾輪選擇器
compile 'com.f1reking.library:wheelview:1.1'
引用library以后可能會報錯洛巢,這個時候你只需要在dependencies后面加上下面代碼:
repositories {
mavenCentral()
maven {
url "https://www.jitpack.io"
}
}
重新編譯饱苟,這個時候就可以正常使用了,還有就是可能會報kotlin的版本問題狼渊,更新一下就行了。
base目錄
很清晰的看出這個目錄主要是一些共用的類的封裝操作类垦。細心的你會發(fā)現(xiàn)目錄中出現(xiàn)了一個MVPActivity狈邑、MVCActivity和MVPFragment、MVCFragment沒錯蚤认,這里為什么做區(qū)分米苹,是因為我們在開發(fā)過程中比如用到閃屏頁往往都是一個靜態(tài)的頁面,無需請求http砰琢,這個時候我們可以用最原始的方法處理就好(MVCActivity或MVCFragment)如果需要請求http(MVPActivity或MVPFragment)蘸嘶。這里我就不粘貼代碼出來了象颖,如果你想了解可以到我的git上下載MCLibrary
BaseRecyclerViewAdapter和BaseReyclerViewHolder是我封裝好的RecyclerView的設配器處理指煎,大大的簡化了以前的代碼。
public class ClassifyAdapter extends BaseRecyclerViewAdapter<ClassifyBean, ClassifyAdapter.ClassifyHolder> {
private Activity mContext;
public ClassifyAdapter(Activity context, int width) {
this.mContext = context;
}
//邏輯處理
@Override
public void onBaseBindViewHolder(final ClassifyAdapter.ClassifyHolder holder, ClassifyBean objectData,
final int position, View itemView, final Boolean isSelect) {
}
//加載界面
@Override
public ClassifyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ClassifyHolder(LayoutInflater.from(mContext).inflate(R.layout.item_classify, parent, false));
}
//holder 初始化控件
class ClassifyHolder extends BaseRecyclerViewHolder {
public ClassifyHolder(View itemView) {
super(itemView);
}
}
}
是不是簡潔了很多凿滤,不在需要那么多繁雜的方法處理挚冤。你只需要將你的實體類和holder傳給BaseRecyclerViewAdapter就行了况增。
http目錄
很明顯這是網(wǎng)絡請求的框架,框架使用了現(xiàn)在最流行的網(wǎng)絡請求框架Rxjava和Retrofit結(jié)合的训挡。由于我們公司提倡模塊化澳骤,所以封裝的網(wǎng)絡請求框架比較適合模塊化操作歧强。
RetrofitServiceManager.instance.create(ClassifyPresenter.ClassifyHttp::class.java)
其中:ClassifyPresenter.ClassifyHttp::class.java參數(shù)是你的類。
RetrofitServiceManager重要代碼如下:
init {
// 創(chuàng)建 OKHttpClient
val builder = OkHttpClient.Builder()
builder.connectTimeout(DEFAULT_TIME_OUT.toLong(), TimeUnit.SECONDS)//連接超時時間
builder.writeTimeout(DEFAULT_READ_TIME_OUT.toLong(), TimeUnit.SECONDS)//寫操作 超時時間
builder.readTimeout(DEFAULT_READ_TIME_OUT.toLong(), TimeUnit.SECONDS)//讀操作超時時間
//添加日志攔截器
builder.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
//修改請求頭
builder.addInterceptor(object : Interceptor {
override fun intercept(chain: Interceptor.Chain?): Response {
val original = chain?.request()
val request = original?.newBuilder()!!
.header("", "")
.header("", )
.header("", )
.header("Content-Type", "application/json; charset=UTF-8")
.method(original.method(), original.body())
.build()
return chain.proceed(request)
}
})
// 創(chuàng)建Retrofit
mRetrofit = Retrofit.Builder()
.client(builder.build())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(Gson()))
.baseUrl(BASE_URL)
.build()
}
在開發(fā)過程中后臺可能要求有些接口是要傳form有些接口是傳json的为肮,我們就需要修改請求頭摊册。我們公司接口是做加密的,所以也可以將加密的數(shù)據(jù)一起寫在請求頭颊艳。是不是很方便茅特。
ObjectLoader主要是對線程進行操作
open class ObjectLoader {
protected fun <T> observe(observable: Observable<T>): Observable<T> {
return observable.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
}
}
大家都知道網(wǎng)絡請求是耗時的操作,修改UI數(shù)據(jù)我們需要在主線程進行操作籽暇。
ApiSubscriberCallBack請求的回調(diào)方法處理温治,當請求報錯的時候,對網(wǎng)絡戒悠、空熬荆、操作失敗等情況的處理。
util主要是一些工具類的這里我就不做多介紹了绸狐,自己看代碼吧卤恳!
widget主要是一些自定義的東西。
表達能力不是很好寒矿,感覺沒什么好說的突琳,因為如果你懂a(chǎn)ndroid代碼,相信你是很容易看得懂這些代碼的符相。自己去github上下載看看吧拆融!有什么不明白之處可以發(fā)表評論看到會回復,如果有什么需要改進的也可以發(fā)表評論啊终,我會急時更新改正镜豹。