简易聊天软件模板-第一行代码
代码
- Menifest:
<!--talk-->
<activity android:name=".talktalk.TalkActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
- Koltin
Msg:
class Msg(val content: String, val type: Int) {
companion object {
const val TYPE_RECEIVED = 0
const val TYPE_SEND = 1
}
}
MsgAdapter:
class MsgAdapter(private val msgList: List<Msg>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
inner class LeftViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val leftMsg: TextView = view.findViewById(R.id.talk_msg_left)
}
inner class RightViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val rightMsg: TextView = view.findViewById(R.id.talk_msg_right)
}
override fun getItemViewType(position: Int): Int {
val msg=msgList[position]
return msg.type
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder =
if (viewType == Msg.TYPE_RECEIVED) {
val view =
LayoutInflater.from(parent.context).inflate(R.layout.msg_left_item, parent, false)
LeftViewHolder(view)
} else {
val view =
LayoutInflater.from(parent.context).inflate(R.layout.msg_right_item, parent, false)
RightViewHolder(view)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val msg = msgList[position]
when (holder) {
is LeftViewHolder -> holder.leftMsg.text = msg.content
is RightViewHolder -> holder.rightMsg.text = msg.content
else -> throw IllegalArgumentException()
}
}
override fun getItemCount() = msgList.size
}
TalkActivity:
class TalkActivity : AppCompatActivity(), View.OnClickListener {
private lateinit var talkBinding: ActivityTalkBinding
private lateinit var adapter: MsgAdapter
private val msgList = ArrayList<Msg>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
talkBinding = ActivityTalkBinding.inflate(layoutInflater)
setContentView(talkBinding.root)
initMsg()
val layoutManager = LinearLayoutManager(this)
talkBinding.talkRecycler.layoutManager = layoutManager
if (!::adapter.isInitialized) {
adapter = MsgAdapter(msgList)
}
talkBinding.talkRecycler.adapter = adapter
talkBinding.talkSend.setOnClickListener(this)
}
override fun onClick(v: View?) {
when (v) {
talkBinding.talkSend -> {
val content = talkBinding.talkInputText.text.toString()
if (content.isNotEmpty()) {
val msg = Msg(content, Msg.TYPE_SEND)
msgList.add(msg)
adapter.notifyItemInserted(msgList.size - 1)//当有新消息时刷新Recycler中的显示
talkBinding.talkRecycler.scrollToPosition(msgList.size - 1)//将RecyclerView定位到最后一行
talkBinding.talkInputText.setText("")//清空输入框
}
}
}
}
private fun initMsg() {
val msg1 = Msg("Hello guy", Msg.TYPE_RECEIVED)
val msg2 = Msg("Hello,Who is that", Msg.TYPE_SEND)
val msg3 = Msg("This is Tom. Nice to meet you. ", Msg.TYPE_RECEIVED)
msgList.apply {
add(msg1)
add(msg2)
add(msg3)
}
}
}
- Layout:
activity_talk:
<?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"
android:background="#d8e0e8">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/talk_recycler"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Type something here"
android:maxLines="2"
android:id="@+id/talk_inputText"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/talk_send"
android:text="Send"/>
</LinearLayout>
</LinearLayout>
msg_left_item:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:background="@drawable/message_left">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/talk_msg_left"
android:layout_gravity="center"
android:layout_margin="10dp"
android:textColor="#fff"/>
</LinearLayout>
</FrameLayout>
msg_right_item:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="@drawable/message_right">
<TextView
android:id="@+id/talk_msg_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:textColor="#000" />
</LinearLayout>
</FrameLayout>
效果: