Skip to content

Commit

Permalink
Workaround for setVisibility temporary selection bug.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
SimplicityApks committed Aug 9, 2015
1 parent f3a50cb commit cbaa35a
Showing 1 changed file with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -93,14 +94,44 @@ public void onRestoreInstanceState(Parcelable state) {
post(new Runnable() {
@Override
public void run() {
if(restoreTemporarySelection)
if (restoreTemporarySelection)
restoreTemporarySelection(tempItem);
}
});
}
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
Expand Down

0 comments on commit cbaa35a

Please sign in to comment.