@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); } }
private void syncMyPreferences() { synchronized (this) { if (mIsSyncing) { return; } mIsSyncing = true; } List<Preference> newPreferenceList = new ArrayList<>(mPreferenceListInternal.size()); flattenPreferenceGroup(newPreferenceList, mPreferenceGroup); mPreferenceListInternal = newPreferenceList; mPreferenceList = new ArrayList<>(mPreferenceListInternal.size()); // Copy only the visible preferences to the active list for (final Preference preference : mPreferenceListInternal) { if (preference.isVisible()) { mPreferenceList.add(preference); } } notifyDataSetChanged(); synchronized (this) { mIsSyncing = false; notifyAll(); } }