Android(Kotlin版本)MVC框架的實體以及代碼
??本文地址:https://blog.csdn.net/qq_40785165/article/details/112135944,轉載需附上此地址
??代碼也許是枯燥的,但是創造永遠能讓人心血澎湃,這大概就是熱情吧!
??大家好,我是小黑,一個還沒禿頭的程式員~~~
??近日較忙,但是也不能忘記了寫文章的初心,就是為了記錄自己一段時間內的學習以及改變,今日內容為Android開發中MVC的框架設計,由于最近在學習kotlin,所以本文章將使用kotlin作為開發語言,先來看看效果圖

??Demo中的功能很簡單,就是個模擬登錄的程序,以及登錄成功后呼叫資料介面開發串列,由于想把MVC架構與資料請求一起寫了,又想節約篇幅,所以登錄模塊的代碼就不貼出來了,這里只展示串列的設計代碼,想要其他代碼的同學可以到Demo的github地址手動下載,話不多說,下面開始正文,
??MVC框架由一下三個部分組成:Model|(模型層)、View(視圖層)、Controller(控制層)
1.Model:負責請求介面,進行資料處理,將結果通過回呼告知Controller層并進行視圖更新,
2.View:視圖設計,在這里一般指layout中的xml視圖代碼
3.Controller:控制層,通常指Activity/Fragment,持有Model物件,通過監聽Model狀態改變進行UI的更新
專案結構如圖所示:

(一)先設計視圖xml檔案即View層,activity_wechat_official_account.xml代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
??串列子項item_office_account.xml代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="40dp" />
</LinearLayout>
(二)、設計Activity代碼,即Controller層,WeChatOfficialAccountListActivity.kt代碼如下:
class WeChatOfficialAccountListActivity : AppCompatActivity(), WeChatOfficeAccountListener {
private val mList: MutableList<OfficeAccountBean> = ArrayList()
private val weChatOfficeAccountModel by lazy {
WeChatOfficeAccountModel()
}
private val weChatOfficialAccountAdapter by lazy {
WeChatOfficialAccountAdapter(this, mList)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_wechat_official_account)
rv_list.layoutManager = LinearLayoutManager(this)
rv_list.adapter = weChatOfficialAccountAdapter
weChatOfficeAccountModel.loadData(this)
}
override fun onl oading() {
Toast.makeText(this, "正在加載中", Toast.LENGTH_SHORT).show()
}
override fun onl oadSuccess(list: MutableList<OfficeAccountBean>?) {
runOnUiThread(Thread {
Toast.makeText(this, "加載成功", Toast.LENGTH_SHORT).show()
if (list != null && list.size > 0) {
mList.addAll(list)
}
weChatOfficialAccountAdapter.notifyDataSetChanged()
})
}
override fun onl oadFail(msg: String?) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
}
}
??從代碼中可以得知,Controller層持有了一個Model物件,并實作了一個介面,實作了其中的回呼,通過回呼進行主執行緒的UI更新,串列的子項配接器WeChatOfficialAccountAdapter.kt代碼如下:
class WeChatOfficialAccountAdapter(var context: Context, var list: MutableList<OfficeAccountBean>) :
RecyclerView.Adapter<WeChatOfficialAccountAdapter.ViewHolder>() {
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
var tvName: TextView = view.findViewById(R.id.tv_name)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(
LayoutInflater.from(parent.context).inflate(
R.layout.item_office_account,
null,
false
)
)
}
override fun getItemCount(): Int {
return list.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.tvName.text = list[position].name
}
}
??回呼的介面WeChatOfficeAccountListener.kt代碼如下:
interface WeChatOfficeAccountListener {
fun onl oading()
fun onl oadSuccess(list: MutableList<OfficeAccountBean>? = null)
fun onl oadFail(msg: String? = "")
}
(三)、設計Model層,實作資料處理,呼叫介面等操作,并通過回呼通知控制層更新UI,WeChatOfficeAccountModel.kt代碼如下:
class WeChatOfficeAccountModel {
private val weChatOfficeAccountApi by lazy {
WeChatOfficeAccountApi()
}
fun loadData(callback: WeChatOfficeAccountListener) {
weChatOfficeAccountApi.loadData(callback)
}
}
??我的Model層里并沒有介面呼叫、資料處理等操作,那是因為我把呼叫介面都放到了WeChatOfficeAccountApi.kt檔案中,代碼如下:
class WeChatOfficeAccountApi {
private val gson by lazy {
GsonBuilder().serializeNulls().create()
}
fun loadData(callback: WeChatOfficeAccountListener) {
callback.onLoading()
//拿到OkhttpClient物件
var okHttpClient = OkHttpClient()
//構造request物件,get方式,傳入介面地址
var url = Request.Builder()
.get()
.url("https://wanandroid.com/wxarticle/chapters/json")
.build()
var newCall = okHttpClient.newCall(url)
newCall.enqueue(object : Callback {
override fun onFailure(call: Call?, e: IOException?) {
callback.onLoadFail()
}
override fun onResponse(call: Call?, response: Response?) {
var string = response?.body()?.string()
Log.e("Tag", string)
var result = gson.fromJson<DataResult<MutableList<OfficeAccountBean>>>(
string,
object : TypeToken<DataResult<MutableList<OfficeAccountBean>>>() {}.type
)
Log.e("Tag", result.toString())
callback.onLoadSuccess(result.data)
}
})
}
}
??上述代碼中,如果不喜歡這種寫法的也可以直接在Model中呼叫介面,因人而異,呼叫的介面是玩Android的開放介面,使用的是okhttp網路請求框架,使用gson決議資料,需要依賴的庫有:
implementation 'com.squareup.okhttp3:okhttp:3.5.0'
implementation 'com.google.code.gson:gson:2.8.6'
??需要的權限有:
<uses-permission android:name="android.permission.INTERNET" />
??好了,到此為止Android的MVC框架就搭建好了,這是我個人對MVC的了解和實踐,有不同意見或者建議的可以在下方評論,我們共同進步,效果圖和源代碼都在開頭!最后,祝大家新年快樂,歲歲歡愉!
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/245189.html
標籤:其他
上一篇:SSM框架入門學習記錄
下一篇:MySQL的體系結構
