将MultiSelectListPreference与PreferenceFragmentCompat一起使用时,与所有带对话框的首选项一样,我们需要使用DialogFragment来显示首选项界面并捕获输入。支持库提供了这样一个对话框片段:MultiSelectListPreferenceDialogFragment
但是,这继承自android.app.DialogFragment继承的android.support.v14.preference.PreferenceDialogFragment。换句话说,它无法使用SupportFragmentManager显示,如果您的应用程序是围绕支持库设计的,那么实际上无法使用MultiSelectList首选项。
幸运的是,我没有理由找到这个,所以一个解决方法就是从MultiSelectListPreferenceDialogFragment和PreferenceDialogFragment中复制'n'粘贴google代码并更改继承以扩展android.support.v4.app.DialogFragment。 / p>
我向谷歌(here)提交了一个错误,代码如下。我正在使用com.android.support:preference-v14:23.3.0
这适用于我的项目,但我不理解将这些支持首选项的使用限制在非支持中心应用程序的逻辑。我知道它在v14支持库中,但其他支持库需要SupportFragmentManager等,并且所有文档都建议使用支持库。有没有更好的方法来解决这个问题?
MultiSelectPrefenceDialog(替换MultiSelectListPreferenceDialogFragment)
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
public class MultiSelectPreferenceDialog extends DialogFragment implements DialogInterface.OnClickListener {
private Set<String> mNewValues = new HashSet<>();
private boolean mPreferenceChanged;
protected static final String ARG_KEY = "key";
private DialogPreference mPreference;
/** Which button was clicked. */
private int mWhichButtonClicked;
public static MultiSelectPreferenceDialog newInstance(String key) {
final MultiSelectPreferenceDialog fragment =
new MultiSelectPreferenceDialog();
final Bundle b = new Bundle(1);
b.putString(ARG_KEY, key);
fragment.setArguments(b);
return fragment;
}
private MultiSelectListPreference getListPreference() {
return (MultiSelectListPreference) getPreference();
}
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
final MultiSelectListPreference preference = getListPreference();
if (preference.getEntries() == null || preference.getEntryValues() == null) {
throw new IllegalStateException(
"MultiSelectListPreference requires an entries array and " +
"an entryValues array.");
}
boolean[] checkedItems = getSelectedItems(preference);
builder.setMultiChoiceItems(preference.getEntries(), checkedItems,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
mPreferenceChanged |= mNewValues.add(
preference.getEntryValues()[which].toString());
} else {
mPreferenceChanged |= mNewValues.remove(
preference.getEntryValues()[which].toString());
}
}
});
mNewValues.clear();
mNewValues.addAll(preference.getValues());
}
public void onDialogClosed(boolean positiveResult) {
final MultiSelectListPreference preference = getListPreference();
if (positiveResult && mPreferenceChanged) {
final Set<String> values = mNewValues;
if (preference.callChangeListener(values)) {
preference.setValues(values);
}
}
mPreferenceChanged = false;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Fragment rawFragment = getTargetFragment();
if (!(rawFragment instanceof DialogPreference.TargetFragment)) {
throw new IllegalStateException("Target fragment must implement TargetFragment" +
" interface");
}
final DialogPreference.TargetFragment fragment =
(DialogPreference.TargetFragment) rawFragment;
final String key = getArguments().getString(ARG_KEY);
mPreference = (DialogPreference) fragment.findPreference(key);
}
@Override
public @NonNull
Dialog onCreateDialog(Bundle savedInstanceState) {
final Context context = getActivity();
mWhichButtonClicked = DialogInterface.BUTTON_NEGATIVE;
final AlertDialog.Builder builder = new AlertDialog.Builder(context)
.setTitle(mPreference.getDialogTitle())
.setIcon(mPreference.getDialogIcon())
.setPositiveButton(mPreference.getPositiveButtonText(), this)
.setNegativeButton(mPreference.getNegativeButtonText(), this);
View contentView = onCreateDialogView(context);
if (contentView != null) {
onBindDialogView(contentView);
builder.setView(contentView);
} else {
builder.setMessage(mPreference.getDialogMessage());
}
onPrepareDialogBuilder(builder);
// Create the dialog
final Dialog dialog = builder.create();
if (needInputMethod()) {
requestInputMethod(dialog);
}
return builder.create();
}
/**
* Get the preference that requested this dialog. Available after {@link #onCreate(Bundle)} has
* been called.
*
* @return The {@link DialogPreference} associated with this
* dialog.
*/
public DialogPreference getPreference() {
return mPreference;
}
/**
* Returns whether the preference needs to display a soft input method when the dialog
* is displayed. Default is false. Subclasses should override this method if they need
* the soft input method brought up automatically.
* @hide
*/
protected boolean needInputMethod() {
return false;
}
/**
* Sets the required flags on the dialog window to enable input method window to show up.
*/
private void requestInputMethod(Dialog dialog) {
Window window = dialog.getWindow();
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
/**
* Creates the content view for the dialog (if a custom content view is
* required). By default, it inflates the dialog layout resource if it is
* set.
*
* @return The content View for the dialog.
* @see DialogPreference#setLayoutResource(int)
*/
protected View onCreateDialogView(Context context) {
final int resId = mPreference.getDialogLayoutResource();
if (resId == 0) {
return null;
}
LayoutInflater inflater = LayoutInflater.from(context);
return inflater.inflate(resId, null);
}
/**
* Binds views in the content View of the dialog to data.
* <p>
* Make sure to call through to the superclass implementation.
*
* @param view The content View of the dialog, if it is custom.
*/
protected void onBindDialogView(View view) {
View dialogMessageView = view.findViewById(android.R.id.message);
if (dialogMessageView != null) {
final CharSequence message = mPreference.getDialogMessage();
int newVisibility = View.GONE;
if (!TextUtils.isEmpty(message)) {
if (dialogMessageView instanceof TextView) {
((TextView) dialogMessageView).setText(message);
}
newVisibility = View.VISIBLE;
}
if (dialogMessageView.getVisibility() != newVisibility) {
dialogMessageView.setVisibility(newVisibility);
}
}
}
@Override
public void onClick(DialogInterface dialog, int which) {
mWhichButtonClicked = which;
}
@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
onDialogClosed(mWhichButtonClicked == DialogInterface.BUTTON_POSITIVE);
}
public boolean[] getSelectedItems(MultiSelectListPreference preference) {
final CharSequence[] entries = preference.getEntryValues();
final int entryCount = entries.length;
final Set<String> values = preference.getValues();
boolean[] result = new boolean[entryCount];
for (int i = 0; i < entryCount; i++) {
result[i] = values.contains(entries[i].toString());
}
return result;
}
}
在PreferenceFragmentCompat中调用对话框片段
@Override
public void onDisplayPreferenceDialog(Preference preference) {
if (preference instanceof MultiSelectListPreference) {
MultiSelectPreferenceDialog multi_fragment = MultiSelectPreferenceDialog.newInstance(preference.getKey());
multi_fragment.setTargetFragment(this, 0);
multi_fragment.show(getFragmentManager(),"MULTISELECT_PREFERENCE_DIALOG");
} else
super.onDisplayPreferenceDialog(preference);
}