@Override public void onPreferenceVisibilityChange(Preference preference) { if (preference.isVisible()) { // The preference has become visible, we need to add it in the correct location. // Index (inferred) in mPreferenceList of the item preceding the newly visible pref int previousVisibleIndex = -1; for (final Preference pref : mPreferenceListInternal) { if (preference.equals(pref)) { break; } if (pref.isVisible()) { previousVisibleIndex++; } } // Insert this preference into the active list just after the previous visible entry mPreferenceList.add(previousVisibleIndex + 1, preference); notifyItemInserted(previousVisibleIndex + 1); } else { // The preference has become invisibile. Find it in the list and remove it. int removalIndex; final int listSize = mPreferenceList.size(); for (removalIndex = 0; removalIndex < listSize; removalIndex++) { if (preference.equals(mPreferenceList.get(removalIndex))) { break; } } mPreferenceList.remove(removalIndex); notifyItemRemoved(removalIndex); } }