From c5b16664bc5e41d63543b6c1ce72bf071d1b23f2 Mon Sep 17 00:00:00 2001 From: kaixed Date: Wed, 18 Dec 2024 22:45:24 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E8=81=8A=E5=A4=A9?= =?UTF-8?q?=E7=95=8C=E9=9D=A2=E5=8F=91=E9=80=81=E6=B6=88=E6=81=AF=E6=97=B6?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E6=97=A0=E6=B3=95=E6=AD=A3=E7=A1=AE=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E5=9C=A8=E5=B1=8F=E5=B9=95=E4=B8=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/kaixed/kchat/ui/activity/ChatActivity.kt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/kaixed/kchat/ui/activity/ChatActivity.kt b/app/src/main/java/com/kaixed/kchat/ui/activity/ChatActivity.kt index 5314674..c5f3214 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/activity/ChatActivity.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/activity/ChatActivity.kt @@ -114,13 +114,14 @@ class ChatActivity : BaseActivity(), OnItemClickListener, super.onCreate(savedInstanceState) enableEdgeToEdge() EventBus.getDefault().register(this) + observeLiveData() firstLoadData() initView() setListener() bindWebSocketService() setPanelChange() getKeyBoardVisibility() - observeViewModel() + observeStateFlow() if (isSearchHistory) { val size = MessagesManager.queryHistory(msgLocalId) binding.recycleChatList.smoothScrollToPosition(size - 1) @@ -134,10 +135,13 @@ class ChatActivity : BaseActivity(), OnItemClickListener, } } - private fun observeViewModel() { + private fun observeStateFlow() { lifecycleScope.launch { MessagesManager.messages.collect { chatAdapter?.submitList(it) + binding.recycleChatList.post { + binding.recycleChatList.smoothScrollToPosition(0) + } } } } @@ -147,7 +151,6 @@ class ChatActivity : BaseActivity(), OnItemClickListener, val binder = service as LocalBinder webSocketService = binder.getService() bound = true - observeLiveData() } override fun onServiceDisconnected(arg0: ComponentName) { @@ -455,7 +458,6 @@ class ChatActivity : BaseActivity(), OnItemClickListener, webSocketService!!.storeOwnerMsg(messages) MessagesManager.sendMessages(messages) - binding.recycleChatList.smoothScrollToPosition(0) binding.etInput.setText("") }