Merge "Restore selected color position" into udc-dev
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
                     }
                 }
             }