diff --git a/.changelog/lang_zh-Hans.json b/.changelog/lang_zh-Hans.json index cb3ef2c8..f836c643 100644 --- a/.changelog/lang_zh-Hans.json +++ b/.changelog/lang_zh-Hans.json @@ -149,6 +149,7 @@ "浮动按钮 \"更多\" 对话框使用异步加载数据方式提升显示流畅度", "浮动按钮 \"运行脚本\" 对话框增加 \"主页\" 菜单项", "浮动按钮 \"运行脚本\" 对话框支持最小化及状态恢复并尽最大努力保持窗口常驻或自动恢复", + "主题色设置页面定位主题色时使用快速定位方式以提升定位效率", "使用 [LiveData](https://developer.android.com/topic/libraries/architecture/livedata) 及 [SharedFlow](https://developer.android.com/kotlin/flow/stateflow-and-sharedflow) 替代已弃用的 [LocalBroadcastManager](https://developer.android.com/jetpack/androidx/releases/localbroadcastmanager)", "Gradle 构建脚本提升 7z 格式文件的解压效率", "Gradle 构建脚本支持获取详细的 Android Studio IDE 版本 (如 \"2025.1.4.7\")", diff --git a/app/src/main/java/org/autojs/autojs/theme/app/ColorItemsActivity.kt b/app/src/main/java/org/autojs/autojs/theme/app/ColorItemsActivity.kt index 6c5c598f..866cb168 100644 --- a/app/src/main/java/org/autojs/autojs/theme/app/ColorItemsActivity.kt +++ b/app/src/main/java/org/autojs/autojs/theme/app/ColorItemsActivity.kt @@ -17,7 +17,6 @@ import androidx.core.view.forEach import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.LinearLayoutManager -import androidx.recyclerview.widget.LinearSmoothScroller import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView.VERTICAL import androidx.recyclerview.widget.ThemeColorRecyclerView @@ -162,27 +161,57 @@ class ColorItemsActivity : ColorSelectBaseActivity() { if (recyclerView.findViewHolderForAdapterPosition(targetPosition) != null) { highlightColorItem(recyclerView, targetPosition, targetItem) } else { - recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() { - override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) { - if (newState == RecyclerView.SCROLL_STATE_IDLE) { - recyclerView.removeOnScrollListener(this) + // @Hint by JetBrains AI Assistant (GPT-5.2) on Feb 3, 2025. + // ! `scrollToPositionWithOffset/scrollToPosition` may not dispatch scroll state changes, + // ! so relying on `SCROLL_STATE_IDLE` can miss the timing. + // ! zh-CN: + // ! `scrollToPositionWithOffset/scrollToPosition` 可能不会触发滚动状态变化, + // ! 因此依赖 `SCROLL_STATE_IDLE` 会错过触发时机. + // ! + // # recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() { + // # override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) { + // # if (newState == RecyclerView.SCROLL_STATE_IDLE) { + // # recyclerView.removeOnScrollListener(this) + // # highlightColorItem(recyclerView, targetPosition, targetItem) + // # } + // # } + // # }) + + // Trigger highlight when the target view is actually attached to window. + // zh-CN: 当目标 View 真正 attach 到窗口时再触发高亮. + val attachListener = object : RecyclerView.OnChildAttachStateChangeListener { + + override fun onChildViewAttachedToWindow(view: View) { + val holder = recyclerView.getChildViewHolder(view) + if (holder.bindingAdapterPosition == targetPosition) { + recyclerView.removeOnChildAttachStateChangeListener(this) highlightColorItem(recyclerView, targetPosition, targetItem) } } - }) + + override fun onChildViewDetachedFromWindow(view: View) = Unit + } + recyclerView.addOnChildAttachStateChangeListener(attachListener) } - val layoutManager = recyclerView.layoutManager + // val layoutManager = recyclerView.layoutManager + // + // val smoothScroller = object : LinearSmoothScroller(this) { + // override fun getVerticalSnapPreference() = SNAP_TO_START + // }.apply { this.targetPosition = targetPosition } + // + // when (layoutManager) { + // null -> recyclerView.smoothScrollToPosition(targetPosition) + // is GridLayoutManager -> layoutManager.startSmoothScroll(smoothScroller) + // is LinearLayoutManager -> layoutManager.startSmoothScroll(smoothScroller) + // else -> layoutManager.startSmoothScroll(smoothScroller) + // } - val smoothScroller = object : LinearSmoothScroller(this) { - override fun getVerticalSnapPreference() = SNAP_TO_START - }.apply { this.targetPosition = targetPosition } - - when (layoutManager) { - null -> recyclerView.smoothScrollToPosition(targetPosition) - is GridLayoutManager -> layoutManager.startSmoothScroll(smoothScroller) - is LinearLayoutManager -> layoutManager.startSmoothScroll(smoothScroller) - else -> layoutManager.startSmoothScroll(smoothScroller) + when (val layoutManager = recyclerView.layoutManager) { + null -> recyclerView.scrollToPosition(targetPosition) + is GridLayoutManager -> layoutManager.scrollToPositionWithOffset(targetPosition, 0) + is LinearLayoutManager -> layoutManager.scrollToPositionWithOffset(targetPosition, 0) + else -> layoutManager.scrollToPosition(targetPosition) } mInitiallyItemIdScrollTo = -1