From cbaa35ac4d1dd3174c9d06ac696fe08d91d29fd7 Mon Sep 17 00:00:00 2001 From: SimplicityApks Date: Sun, 9 Aug 2015 21:24:07 +0200 Subject: [PATCH] Workaround for setVisibility temporary selection bug. When rotating the device with FLAG_HIDE_TIME set and a temporary item selected, the footer would be selected after clicking on the time button. It turns out the system somehow manages to get rid of the temporary selection after the Spinner is made visible. --- .../reminderdatepicker/lib/PickerSpinner.java | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/src/main/java/com/simplicityapks/reminderdatepicker/lib/PickerSpinner.java b/lib/src/main/java/com/simplicityapks/reminderdatepicker/lib/PickerSpinner.java index 839da0a..fa925f4 100644 --- a/lib/src/main/java/com/simplicityapks/reminderdatepicker/lib/PickerSpinner.java +++ b/lib/src/main/java/com/simplicityapks/reminderdatepicker/lib/PickerSpinner.java @@ -5,6 +5,7 @@ import android.os.Parcelable; import android.support.annotation.NonNull; import android.util.AttributeSet; +import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.Spinner; @@ -93,7 +94,7 @@ public void onRestoreInstanceState(Parcelable state) { post(new Runnable() { @Override public void run() { - if(restoreTemporarySelection) + if (restoreTemporarySelection) restoreTemporarySelection(tempItem); } }); @@ -101,6 +102,36 @@ public void run() { else super.onRestoreInstanceState(state); } + /** + * {@inheritDoc} + */ + @Override + public void setVisibility(int visibility) { + super.setVisibility(visibility); + // When going from state gone to visible with a temporary item selected, the position is + // somehow reset by the system, so we need to reselect the temporary item. + // This is merely a workaround as I can't find a better solution. + if(visibility == VISIBLE) { + PickerSpinnerAdapter adapter = (PickerSpinnerAdapter) getAdapter(); + int count = adapter.getCount(); + // check whether we have the temporary item selected + if(getSelectedItemPosition() == count) { + // get the temp item from the adapter to reselect it later: + TwinTextItem tempItem = null; + try { + tempItem = adapter.getItem(count); + } catch (IndexOutOfBoundsException e) { + Log.d("PickerSpinner", "SetVisibility: Couldn't get temporary item from adapter, aborting workaround"); + } + // now reselect the temporary item + if(tempItem != null) + selectTemporary(tempItem); + } + } + + } + + /** * Sets the Adapter used to provide the data which backs this Spinner. Needs to be an {@link com.simplicityapks.reminderdatepicker.lib.PickerSpinnerAdapter} * to be used with this class. Note that a PickerSpinner automatically creates its own adapter