第一行代碼3章最佳實踐kotlin版本

很久沒有更新過編程方面的知識了,大家(包括我自己)都忘了本號還有技術方面的內容,今天只是給大家分享個例子,算是預祝郭霖大佬新書《第一行代碼》kotlin版本成功定版。

代碼我只會簡單的介紹,具體講解留給郭霖大佬吧,我就不顯醜了。

第一行代碼3章最佳實踐kotlin版本

以上是做完的結果,和書裡的例子基本一樣,直接來實際的,上代碼:

佈局代碼沒有改變,我就不說了(最後代碼會上傳雲盤,末尾有鏈接),還是說有變化的吧,首先是msg文件:

<code>
package com.example.mytalke

import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView


class MainActivity : AppCompatActivity() {
private var msgList= mutableListOf()

private var inputText: EditText? = null

private var send: Button? = null

private var msgRecyclerView: RecyclerView? = null

private var adapter: MsgAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initMsgs()
inputText=findViewById(R.id.input_text)
send=findViewById(R.id.send) as Button
msgRecyclerView=findViewById(R.id.msg_recycler_view) as RecyclerView
val layoutManager = LinearLayoutManager(this)
msgRecyclerView!!.layoutManager = layoutManager
adapter = MsgAdapter(msgList)
msgRecyclerView!!.adapter = adapter
send!!.setOnClickListener(object : View.OnClickListener{
override fun onClick(v: View?) {
val content = inputText!!.getText().toString()

if ("" != content) {
val msg = Msg(content, Msg.TYPE_SENT)
msgList.add(msg)
adapter!!.notifyItemInserted(msgList.size - 1) // 當有新消息時,刷新ListView中的顯示
msgRecyclerView!!.scrollToPosition(msgList.size - 1) // 將ListView定位到最後一行
inputText!!.setText("") // 清空輸入框中的內容
}
}
})
}
private fun initMsgs() {
val msg1 = Msg("Hello guy.", Msg.TYPE_RECEIVED)
msgList.add(msg1)
val msg2 = Msg("Hello. Who is that?", Msg.TYPE_SENT)
msgList.add(msg2)
val msg3 = Msg("This is Tom. Nice talking to you. ", Msg.TYPE_RECEIVED)
msgList.add(msg3)
}
}
/<code>

我說點個人的理解,kotlin和java的區別在上面的代碼裡主要是創建list時有不同,賦值時有變化,還有要注意kotlin的空安全,java裡可以直接先創建變量不賦值,但kotlin裡必須要賦值,並且設置為可空變量。

接著是MsgAdapter:

<code>
package com.example.mytalke


import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView


class MsgAdapter(private val mMsgList: List) :RecyclerView.Adapter<msgadapter.viewholder>() {


open class Viewholder(view: View) : RecyclerView.ViewHolder(view) {
var leftLayout: LinearLayout
var rightLayout: LinearLayout
var leftMsg: TextView
var rightMsg: TextView

init {
leftLayout = view.findViewById<view>(R.id.left_layout) as LinearLayout
rightLayout = view.findViewById<view>(R.id.right_layout) as LinearLayout
leftMsg = view.findViewById<view>(R.id.left_msg) as TextView
rightMsg = view.findViewById<view>(R.id.right_msg) as TextView
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MsgAdapter.Viewholder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.msg_item, parent, false)
return Viewholder(view)
}

override fun onBindViewHolder(holder: Viewholder, position: Int) {
val msg = mMsgList[position]
if (msg.type == Msg.TYPE_RECEIVED) {
// 如果是收到的消息,則顯示左邊的消息佈局,將右邊的消息佈局隱藏
holder.leftLayout.visibility = View.VISIBLE
holder.rightLayout.visibility = View.GONE
holder.leftMsg.text = msg.content
} else if (msg.type == Msg.TYPE_SENT) {
// 如果是發出的消息,則顯示右邊的消息佈局,將左邊的消息佈局隱藏
holder.rightLayout.visibility = View.VISIBLE
holder.leftLayout.visibility = View.GONE
holder.rightMsg.text = msg.content
}
}

override fun getItemCount(): Int {
return mMsgList.size
}

}/<view>/<view>/<view>/<view>/<msgadapter.viewholder>
/<code>

這段代碼需要注意的就是各個繼承: 不同的Viewholder ,你一定要分清到底繼承自那個holder。

主程序,主要也是注意空安全

<code>
package com.example.mytalke

import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView


class MainActivity : AppCompatActivity() {
private var msgList= mutableListOf()

private var inputText: EditText? = null

private var send: Button? = null

private var msgRecyclerView: RecyclerView? = null

private var adapter: MsgAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initMsgs()
inputText=findViewById(R.id.input_text)
send=findViewById(R.id.send) as Button
msgRecyclerView=findViewById(R.id.msg_recycler_view) as RecyclerView
val layoutManager = LinearLayoutManager(this)
msgRecyclerView!!.layoutManager = layoutManager
adapter = MsgAdapter(msgList)
msgRecyclerView!!.adapter = adapter
send!!.setOnClickListener(object : View.OnClickListener{
override fun onClick(v: View?) {
val content = inputText!!.getText().toString()
if ("" != content) {
val msg = Msg(content, Msg.TYPE_SENT)
msgList.add(msg)
adapter!!.notifyItemInserted(msgList.size - 1) // 當有新消息時,刷新ListView中的顯示
msgRecyclerView!!.scrollToPosition(msgList.size - 1) // 將ListView定位到最後一行
inputText!!.setText("") // 清空輸入框中的內容

}
}
})
}
private fun initMsgs() {
val msg1 = Msg("Hello guy.", Msg.TYPE_RECEIVED)
msgList.add(msg1)
val msg2 = Msg("Hello. Who is that?", Msg.TYPE_SENT)
msgList.add(msg2)
val msg3 = Msg("This is Tom. Nice talking to you. ", Msg.TYPE_RECEIVED)
msgList.add(msg3)
}
}
/<code>

最後附上雲盤地址,裡面有本例子的資源代碼

<code>
鏈接:https://pan.baidu.com/s/13H8noSiO77B05CS5SASVVw
提取碼:l3gr/<code>


分享到:


相關文章: