Merge "Restore selected color position" into udc-dev am: be6f23f033

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/ThemePicker/+/22889905

Change-Id: I63d3964f4c922c9ef3613f2f7b9b9f1134ee5b41
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/src/com/android/customization/picker/color/ui/binder/ColorPickerBinder.kt b/src/com/android/customization/picker/color/ui/binder/ColorPickerBinder.kt
index 3dba27e..3eadec5 100644
--- a/src/com/android/customization/picker/color/ui/binder/ColorPickerBinder.kt
+++ b/src/com/android/customization/picker/color/ui/binder/ColorPickerBinder.kt
@@ -92,6 +92,22 @@
                 launch {
                     viewModel.colorOptions.collect { colorOptions ->
                         colorOptionAdapter.setItems(colorOptions)
+                        // the same recycler view is used for different color types tabs
+                        // the scroll state of each tab should be independent of others
+                        var indexToFocus = 0
+                        colorOptions.forEachIndexed { index, colorOption ->
+                            if (colorOption.isSelected.value) {
+                                indexToFocus = index
+                            }
+                        }
+                        val linearLayoutManager =
+                            object : LinearLayoutManager(view.context, HORIZONTAL, false) {
+                                override fun onLayoutCompleted(state: RecyclerView.State?) {
+                                    super.onLayoutCompleted(state)
+                                    scrollToPosition(indexToFocus)
+                                }
+                            }
+                        colorOptionContainerView.layoutManager = linearLayoutManager
                     }
                 }
             }