From 5ace636d8077fbf23870cffdeec0eb0012b45890 Mon Sep 17 00:00:00 2001 From: Kudo Chien Date: Fri, 10 May 2019 12:51:33 +0800 Subject: [PATCH] Fix PickerAndroid will reset selected value during items update. Summary: Two root causes: 1. Android Spinner will reset selection to undefined after setAdapter() which will trigger onValueChange(). The behavior is not expected for RN. And the solution is to setSelection() explicitly 2. In original implementation, it setups `items` immediately, but delays the `selected` after update transaction. There will be some race condition and incosistency if update `items` only. The fix will update both after transaction. Fixes #13351 --- .../react/views/picker/ReactPicker.java | 47 ++++++++++++------- .../views/picker/ReactPickerManager.java | 6 +-- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/picker/ReactPicker.java b/ReactAndroid/src/main/java/com/facebook/react/views/picker/ReactPicker.java index 09498f3175d40f..a030a454908618 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/picker/ReactPicker.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/picker/ReactPicker.java @@ -13,6 +13,7 @@ import android.view.View; import android.widget.AdapterView; import android.widget.Spinner; +import android.widget.SpinnerAdapter; import com.facebook.react.common.annotations.VisibleForTesting; @@ -23,6 +24,7 @@ public class ReactPicker extends AppCompatSpinner { private int mMode = Spinner.MODE_DIALOG; private @Nullable Integer mPrimaryColor; private @Nullable OnSelectListener mOnSelectListener; + private @Nullable SpinnerAdapter mStagedAdapter; private @Nullable Integer mStagedSelection; private final OnItemSelectedListener mItemSelectedListener = new OnItemSelectedListener() { @@ -111,33 +113,42 @@ public void setOnSelectListener(@Nullable OnSelectListener onSelectListener) { return mOnSelectListener; } + /* package */ void setStagedAdapter(final SpinnerAdapter adapter) { + mStagedAdapter = adapter; + } + /** - * Will cache "selection" value locally and set it only once {@link #updateStagedSelection} is + * Will cache "selection" value locally and set it only once {@link #commitStagedData} is * called */ - public void setStagedSelection(int selection) { + /* package */ void setStagedSelection(int selection) { mStagedSelection = selection; } - public void updateStagedSelection() { - if (mStagedSelection != null) { - setSelectionWithSuppressEvent(mStagedSelection); - mStagedSelection = null; - } - } - /** - * Set the selection while suppressing the follow-up {@link OnSelectListener#onItemSelected(int)} - * event. This is used so we don't get an event when changing the selection ourselves. - * - * @param position the position of the selected item + * Used to commit staged data into ReactPicker view. + * During this period, we will disable {@link OnSelectListener#onItemSelected(int)} temporarily, + * so we don't get an event when changing the items/selection ourselves. */ - private void setSelectionWithSuppressEvent(int position) { - if (position != getSelectedItemPosition()) { - setOnItemSelectedListener(null); - setSelection(position, false); - setOnItemSelectedListener(mItemSelectedListener); + /* package */ void commitStagedData() { + setOnItemSelectedListener(null); + + final int origSelection = getSelectedItemPosition(); + if (mStagedAdapter != null && mStagedAdapter != getAdapter()) { + setAdapter(mStagedAdapter); + // After setAdapter(), Spinner will reset selection and cause unnecessary onValueChange event. + // Explicitly setup selection again to prevent this. + // Ref: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget/AbsSpinner.java#123 + setSelection(origSelection, false); + mStagedAdapter = null; } + + if (mStagedSelection != null && mStagedSelection != origSelection) { + setSelection(mStagedSelection, false); + mStagedSelection = null; + } + + setOnItemSelectedListener(mItemSelectedListener); } public @Nullable Integer getPrimaryColor() { diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/picker/ReactPickerManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/picker/ReactPickerManager.java index db26da8e0a28e6..9e70d661f33c92 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/picker/ReactPickerManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/picker/ReactPickerManager.java @@ -44,9 +44,9 @@ public void setItems(ReactPicker view, @Nullable ReadableArray items) { } ReactPickerAdapter adapter = new ReactPickerAdapter(view.getContext(), data); adapter.setPrimaryTextColor(view.getPrimaryColor()); - view.setAdapter(adapter); + view.setStagedAdapter(adapter); } else { - view.setAdapter(null); + view.setStagedAdapter(null); } } @@ -77,7 +77,7 @@ public void setSelected(ReactPicker view, int selected) { @Override protected void onAfterUpdateTransaction(ReactPicker view) { super.onAfterUpdateTransaction(view); - view.updateStagedSelection(); + view.commitStagedData(); } @Override