refactor(chat): 优化聊天界面滚动逻辑并清理欢迎页面VM代码

- 使用 scrollToPositionWithOffset 替代复杂的两帧滚动实现
- 移除不必要的 post 延迟操作和手动位置调整
- 删除 WelcomeVM 中未使用的网络请求相关代码
- 移除过时的 MutableLiveData 和 HTTP 相关导入
- 简化 ViewModel 结构,提升代码可维护性
这个提交包含在:
徐勤民 2026-04-16 22:47:57 +08:00
父节点 d9b612028a
当前提交 b7f2405b7b
共有 2 个文件被更改,包括 2 次插入40 次删除

查看文件

@ -62,14 +62,8 @@ class ChatActivity : BaseListFormLayoutNormalActivity<ChatItem, ChatVM, Activity
val lastIndex = (recyclerView.adapter?.itemCount ?: 1) - 1 val lastIndex = (recyclerView.adapter?.itemCount ?: 1) - 1
if (lastIndex < 0) return if (lastIndex < 0) return
val lm = recyclerView.layoutManager as? androidx.recyclerview.widget.LinearLayoutManager ?: return val lm = recyclerView.layoutManager as? androidx.recyclerview.widget.LinearLayoutManager ?: return
// 第一帧:确保 lastIndex 进入可视区域 // 将最新 item 顶部对齐到 RecyclerView 顶部
lm.scrollToPosition(lastIndex) lm.scrollToPositionWithOffset(lastIndex, 0)
// 第二帧layout 完成后,把 lastView 的底部对齐到 RecyclerView 底部
recyclerView.post {
val lastView = lm.findViewByPosition(lastIndex) ?: return@post
val gap = lastView.bottom - (recyclerView.height - recyclerView.paddingBottom)
if (gap > 0) recyclerView.scrollBy(0, gap)
}
} }
override fun adapter() = object : CommonPagedAdapter<ChatItem>(R.layout.item_chat) { override fun adapter() = object : CommonPagedAdapter<ChatItem>(R.layout.item_chat) {

查看文件

@ -1,39 +1,7 @@
package com.nova.brain.glass.viewmodel package com.nova.brain.glass.viewmodel
import androidx.lifecycle.MutableLiveData
import com.nova.brain.glass.repository.Service
import com.xuqm.base.App
import com.xuqm.base.di.manager.HttpManager
import com.xuqm.sdhbwfu.core.viewModel.BaseViewModel import com.xuqm.sdhbwfu.core.viewModel.BaseViewModel
import io.reactivex.schedulers.Schedulers
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
class WelcomeVM : BaseViewModel() { class WelcomeVM : BaseViewModel() {
val result = MutableLiveData<String>()
fun demoGet() {
result.value = "GET 请求中..."
HttpManager.getApi(Service::class.java).demoGet()
.subscribeOn(Schedulers.io())
.subscribe({ body ->
result.postValue("GET 响应:\n${body.string()}")
}, { e ->
result.postValue("GET 失败: ${e.message}")
}).adds()
}
fun demoPost() {
result.value = "POST 请求中..."
val json = """{"demo":"post","from":"glass"}"""
val body = RequestBody.create("application/json".toMediaTypeOrNull(), json)
HttpManager.getApi(Service::class.java).demoPost(body)
.subscribeOn(Schedulers.io())
.subscribe({ resp ->
result.postValue("POST 响应:\n${resp.string()}")
}, { e ->
result.postValue("POST 失败: ${e.message}")
}).adds()
}
} }