Fix Android monitor scroll stability

This commit is contained in:
Codex
2026-06-26 22:21:24 +03:00
parent 9c983ac0bb
commit 4e811daaf2
2 changed files with 33 additions and 7 deletions
+2 -2
View File
@@ -10,7 +10,7 @@ android {
applicationId = "xyz.kusoft.tradebotmonitor"
minSdk = 26
targetSdk = 36
versionCode = 1
versionName = "0.1.0"
versionCode = 2
versionName = "0.1.1"
}
}
@@ -39,6 +39,7 @@ class MainActivity : Activity() {
private var lastError: String = ""
private var activeTab: String = "markets"
private var palette: AppPalette = AppPalette.dark()
private val scrollPositions = mutableMapOf<String, Int>()
private val refreshRunnable = object : Runnable {
override fun run() {
@@ -127,9 +128,12 @@ class MainActivity : Activity() {
).forEach { (id, title) ->
tabBar.addView(
button(title, selected = activeTab == id) {
activeTab = id
renderTabs()
render()
if (activeTab != id) {
saveCurrentScroll()
activeTab = id
renderTabs()
render(saveScroll = false)
}
},
LinearLayout.LayoutParams(0, dp(38), 1f).apply { setMargins(dp(3), 0, dp(3), 0) },
)
@@ -150,7 +154,11 @@ class MainActivity : Activity() {
styleLabel(statusView, if (data.ok && data.running) "ok" else "warn")
}
private fun render() {
private fun render(saveScroll: Boolean = true) {
val tabForRender = activeTab
if (saveScroll) {
saveCurrentScroll(tabForRender)
}
contentHost.removeAllViews()
val view = when (activeTab) {
"overview" -> overviewScreen()
@@ -159,6 +167,23 @@ class MainActivity : Activity() {
else -> marketsScreen()
}
contentHost.addView(view)
restoreScroll(view, tabForRender)
}
private fun saveCurrentScroll(tabId: String = activeTab) {
val current = contentHost.getChildAt(0)
if (current is ScrollView) {
scrollPositions[tabId] = current.scrollY
}
}
private fun restoreScroll(view: View, tabId: String) {
if (view !is ScrollView) return
val savedY = scrollPositions[tabId] ?: 0
view.post {
val maxY = (view.getChildAt(0)?.height ?: 0) - view.height
view.scrollTo(0, savedY.coerceIn(0, maxOf(0, maxY)))
}
}
private fun overviewScreen(): View {
@@ -610,7 +635,8 @@ class MainActivity : Activity() {
background = boxDrawable(if (selected) palette.blue else palette.panelAlt, if (selected) palette.blue else palette.line, 5)
setOnClickListener { onClick() }
isClickable = true
isFocusable = true
isFocusable = false
isFocusableInTouchMode = false
}
private fun label(title: String, kind: String): TextView =