refactor(chat): 优化聊天界面滚动逻辑并清理欢迎页面VM代码
- 使用 scrollToPositionWithOffset 替代复杂的两帧滚动实现 - 移除不必要的 post 延迟操作和手动位置调整 - 删除 WelcomeVM 中未使用的网络请求相关代码 - 移除过时的 MutableLiveData 和 HTTP 相关导入 - 简化 ViewModel 结构,提升代码可维护性
这个提交包含在:
父节点
d9b612028a
当前提交
b7f2405b7b
@ -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()
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
正在加载...
在新工单中引用
屏蔽一个用户