Skip to content

Commit

Permalink
UI优化完成。
Browse files Browse the repository at this point in the history
  • Loading branch information
lilongweidev committed Aug 8, 2022
1 parent b73738e commit 035b5aa
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 25 additions & 25 deletions app/src/main/java/com/llw/socket/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.llw.socket

import android.content.Intent
import android.net.wifi.WifiManager
import android.os.Bundle
import android.util.Log
Expand Down Expand Up @@ -32,7 +33,7 @@ class MainActivity : AppCompatActivity(), ServerCallback, ClientCallback {

//消息列表
private val messages = ArrayList<Message>()

//消息适配器
private lateinit var msgAdapter: MsgAdapter

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -62,7 +63,7 @@ class MainActivity : AppCompatActivity(), ServerCallback, ClientCallback {
SocketServer.stopServer();false
} else SocketServer.startServer(this)
//显示日志
showInfo(if (openSocket) "开启服务" else "关闭服务")
showMsg(if (openSocket) "开启服务" else "关闭服务")
//改变按钮文字
binding.btnStartService.text = if (openSocket) "关闭服务" else "开启服务"
}
Expand All @@ -77,34 +78,30 @@ class MainActivity : AppCompatActivity(), ServerCallback, ClientCallback {
} else {
SocketClient.connectServer(ip, this);true
}
showInfo(if (connectSocket) "连接服务" else "关闭连接")
showMsg(if (connectSocket) "连接服务" else "关闭连接")
binding.btnConnectService.text = if (connectSocket) "关闭连接" else "连接服务"
}
//发送消息 给 服务端/客户端
binding.btnSendMsg.setOnClickListener {
val msg = binding.etMsg.text.toString()
val msg = binding.etMsg.text.toString().trim()
if (msg.isEmpty()) {
showMsg("请输入要发送的信息");return@setOnClickListener
}
//检查是否能发送消息
val isSend = if (openSocket) { openSocket} else if (connectSocket) { connectSocket } else { false }
val isSend = if (openSocket) openSocket else if (connectSocket) connectSocket else false
if (!isSend) {
showMsg("当前未开启服务或连接服务");return@setOnClickListener
}
if (isServer) SocketServer.sendToClient(msg) else SocketClient.sendToServer(msg)
binding.etMsg.setText("")
updateList(if (isServer) 1 else 2, msg)
}

//初始化列表
msgAdapter = MsgAdapter(messages)
binding.rvMsg.layoutManager = LinearLayoutManager(this)
binding.rvMsg.adapter = msgAdapter
}

private fun showInfo(info: String) {
buffer.append(info).append("\n")
binding.tvInfo.text = buffer.toString()
binding.rvMsg.apply {
layoutManager = LinearLayoutManager(this@MainActivity)
adapter = msgAdapter
}
}

private fun getIp() =
Expand All @@ -116,26 +113,29 @@ class MainActivity : AppCompatActivity(), ServerCallback, ClientCallback {
/**
* 接收到客户端发的消息
*/
override fun receiveClientMsg(success: Boolean, msg: String) {
showInfo("ClientMsg: $msg")
updateList(2, msg)
}

override fun otherMsg(msg: String) {
showInfo(msg)
}
override fun receiveClientMsg(success: Boolean, msg: String) = updateList(2, msg)

/**
* 接收到服务端发的消息
*/
override fun receiveServerMsg(msg: String) {
showInfo("ServerMsg: $msg")
updateList(1, msg)
override fun receiveServerMsg(msg: String) = updateList(1, msg)


override fun otherMsg(msg: String) {
Log.d(TAG, msg)
}

/**
* 更新列表
*/
private fun updateList(type: Int, msg: String) {
messages.add(Message(type, msg))
runOnUiThread { msgAdapter.notifyItemChanged(if (messages.size == 0) 0 else messages.size - 1) }
runOnUiThread {
(if (messages.size == 0) 0 else messages.size - 1).apply {
msgAdapter.notifyItemChanged(this)
binding.rvMsg.smoothScrollToPosition(this)
}
}
}

private fun showMsg(msg: String) = Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
Expand Down
10 changes: 2 additions & 8 deletions app/src/main/java/com/llw/socket/MsgAdapter.kt
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
package com.llw.socket

import android.content.Context
import android.opengl.Visibility
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.content.res.AppCompatResources.getDrawable
import androidx.core.content.ContextCompat
import androidx.core.content.ContextCompat.getColor
import androidx.recyclerview.widget.RecyclerView
import com.llw.socket.databinding.ItemRvMsgBinding

class MsgAdapter(private val messages: ArrayList<Message>) :
RecyclerView.Adapter<MsgAdapter.ViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
ViewHolder(ItemRvMsgBinding.inflate(LayoutInflater.from(parent.context), parent, false))

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(ItemRvMsgBinding.inflate(LayoutInflater.from(parent.context)))
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val message = messages[position]
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/drawable/shape_et_bg.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="20dp"/>
<solid android:color="#EEE"/>
<corners android:radius="20dp" />
<solid android:color="#EEE" />
</shape>
7 changes: 0 additions & 7 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,6 @@
app:layout_constraintTop_toBottomOf="@+id/btn_start_service" />
</LinearLayout>

<TextView
android:id="@+id/tv_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="信息" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_msg"
android:layout_width="match_parent"
Expand Down
3 changes: 0 additions & 3 deletions app/src/main/res/layout/item_rv_msg.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black"
android:orientation="horizontal"
android:paddingTop="8dp"
android:paddingBottom="8dp"
Expand Down

0 comments on commit 035b5aa

Please sign in to comment.