Skip to content

Commit

Permalink
Restore the enable state of list items after an orientation change.
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-rapp committed Dec 29, 2019
1 parent b201128 commit d41d0a3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public void run() {
* @return True, if the enable state of the item at the given position has changed, false
* otherwise
*/
public final boolean setItemEnabledInternally(final int position, final boolean enabled) {
private boolean setItemEnabledInternally(final int position, final boolean enabled) {
boolean result = false;

if (enabled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ public class ListDialogDecorator extends AbstractDialogDecorator<ButtonBarDialog
private static final String CHECKED_ITEMS_EXTRA =
ListDialogDecorator.class.getSimpleName() + "::checkedItems";

/**
* The name of the extra, which is used to store the enabled items within a bundle.
*/
private static final String ENABLED_ITEMS_EXTRA =
ListDialogDecorator.class.getSimpleName() + "::enabledItems";

/**
* The list view, which is used to show the dialog's list items.
*/
Expand Down Expand Up @@ -264,6 +270,27 @@ private boolean[] getCheckedItems() {
return null;
}

/**
* Returns an array, which identifies the currently enabled list items.
*
* @return An array, which identifies the currently enabled list items, as a {@link Boolean}
* array
*/
@Nullable
private boolean[] getEnabledItems() {
if (adapter != null) {
boolean[] result = new boolean[adapter.getItemCount()];

for (int i = 0; i < result.length; i++) {
result[i] = adapter.isItemEnabled(i);
}

return result;
}

return null;
}

/**
* Returns the index of the first checked item.
*
Expand Down Expand Up @@ -353,6 +380,19 @@ private void adaptItemIconTintMode() {
}
}

/**
* Adapts the enabled items.
*
* @param enabledItems An array, which indicates the enabled items, as a {@link Boolean} array
*/
private void adaptEnabledItems(@Nullable final boolean[] enabledItems) {
if (enabledItems != null && adapter != null) {
for (int i = 0; i < enabledItems.length; i++) {
adapter.setItemEnabled(i, enabledItems[i]);
}
}
}

/**
* Creates a new decorator, which allows to modify the view hierarchy of a dialog, which is
* designed according to Android 5's Material Design guidelines even on pre-Lollipop devices and
Expand Down Expand Up @@ -701,12 +741,15 @@ public final void onSaveInstanceState(@NonNull final Bundle outState) {

if (items != null) {
outState.putCharSequenceArray(ITEMS_EXTRA, items);
outState.putBooleanArray(ENABLED_ITEMS_EXTRA, getEnabledItems());
} else if (singleChoiceItems != null) {
outState.putCharSequenceArray(SINGLE_CHOICE_ITEMS_EXTRA, singleChoiceItems);
outState.putBooleanArray(CHECKED_ITEMS_EXTRA, getCheckedItems());
outState.putBooleanArray(ENABLED_ITEMS_EXTRA, getEnabledItems());
} else if (multiChoiceItems != null) {
outState.putCharSequenceArray(MULTI_CHOICE_ITEMS_EXTRA, multiChoiceItems);
outState.putBooleanArray(CHECKED_ITEMS_EXTRA, getCheckedItems());
outState.putBooleanArray(ENABLED_ITEMS_EXTRA, getEnabledItems());
}
}

Expand All @@ -722,23 +765,28 @@ public final void onRestoreInstanceState(@NonNull final Bundle savedInstanceStat
int[] iconResourceIds = savedInstanceState.getIntArray(ICON_RESOURCE_IDS_EXTRA);

if (items != null) {
boolean[] enabledItems = savedInstanceState.getBooleanArray(ENABLED_ITEMS_EXTRA);
setItems(items, iconResourceIds, this.singleChoiceListener);
adaptEnabledItems(enabledItems);
} else {
boolean[] checkedItems = savedInstanceState.getBooleanArray(CHECKED_ITEMS_EXTRA);
boolean[] enabledItems = savedInstanceState.getBooleanArray(ENABLED_ITEMS_EXTRA);
CharSequence[] singleChoiceItems =
savedInstanceState.getCharSequenceArray(SINGLE_CHOICE_ITEMS_EXTRA);

if (singleChoiceItems != null) {
int checkedItem = checkedItems != null ? indexOfCheckedItem(checkedItems) : -1;
setSingleChoiceItems(singleChoiceItems, iconResourceIds, checkedItem,
this.singleChoiceListener);
adaptEnabledItems(enabledItems);
} else {
CharSequence[] multiChoiceItems =
savedInstanceState.getCharSequenceArray(MULTI_CHOICE_ITEMS_EXTRA);

if (multiChoiceItems != null) {
setMultiChoiceItems(multiChoiceItems, iconResourceIds, checkedItems,
this.multiChoiceListener);
adaptEnabledItems(enabledItems);
}
}
}
Expand Down

0 comments on commit d41d0a3

Please sign in to comment.