6.7.0 - Alpha19 - 主题色设置页面定位主题色时使用快速定位方式以提升定位效率
This commit is contained in:
@@ -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)",
|
"使用 [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 构建脚本提升 7z 格式文件的解压效率",
|
||||||
"Gradle 构建脚本支持获取详细的 Android Studio IDE 版本 (如 \"2025.1.4.7\")",
|
"Gradle 构建脚本支持获取详细的 Android Studio IDE 版本 (如 \"2025.1.4.7\")",
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ import androidx.core.view.forEach
|
|||||||
import androidx.recyclerview.widget.DividerItemDecoration
|
import androidx.recyclerview.widget.DividerItemDecoration
|
||||||
import androidx.recyclerview.widget.GridLayoutManager
|
import androidx.recyclerview.widget.GridLayoutManager
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
import androidx.recyclerview.widget.LinearSmoothScroller
|
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import androidx.recyclerview.widget.RecyclerView.VERTICAL
|
import androidx.recyclerview.widget.RecyclerView.VERTICAL
|
||||||
import androidx.recyclerview.widget.ThemeColorRecyclerView
|
import androidx.recyclerview.widget.ThemeColorRecyclerView
|
||||||
@@ -162,27 +161,57 @@ class ColorItemsActivity : ColorSelectBaseActivity() {
|
|||||||
if (recyclerView.findViewHolderForAdapterPosition(targetPosition) != null) {
|
if (recyclerView.findViewHolderForAdapterPosition(targetPosition) != null) {
|
||||||
highlightColorItem(recyclerView, targetPosition, targetItem)
|
highlightColorItem(recyclerView, targetPosition, targetItem)
|
||||||
} else {
|
} else {
|
||||||
recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
|
// @Hint by JetBrains AI Assistant (GPT-5.2) on Feb 3, 2025.
|
||||||
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
|
// ! `scrollToPositionWithOffset/scrollToPosition` may not dispatch scroll state changes,
|
||||||
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
|
// ! so relying on `SCROLL_STATE_IDLE` can miss the timing.
|
||||||
recyclerView.removeOnScrollListener(this)
|
// ! 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)
|
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) {
|
when (val layoutManager = recyclerView.layoutManager) {
|
||||||
override fun getVerticalSnapPreference() = SNAP_TO_START
|
null -> recyclerView.scrollToPosition(targetPosition)
|
||||||
}.apply { this.targetPosition = targetPosition }
|
is GridLayoutManager -> layoutManager.scrollToPositionWithOffset(targetPosition, 0)
|
||||||
|
is LinearLayoutManager -> layoutManager.scrollToPositionWithOffset(targetPosition, 0)
|
||||||
when (layoutManager) {
|
else -> layoutManager.scrollToPosition(targetPosition)
|
||||||
null -> recyclerView.smoothScrollToPosition(targetPosition)
|
|
||||||
is GridLayoutManager -> layoutManager.startSmoothScroll(smoothScroller)
|
|
||||||
is LinearLayoutManager -> layoutManager.startSmoothScroll(smoothScroller)
|
|
||||||
else -> layoutManager.startSmoothScroll(smoothScroller)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mInitiallyItemIdScrollTo = -1
|
mInitiallyItemIdScrollTo = -1
|
||||||
|
|||||||
Reference in New Issue
Block a user