我在ListPreference中设置类别列表时遇到了类似的问题。问题是您无法通过setEntries方法更改带有列表的对话框中的可见选项(在单击侦听器中设置条目为时已晚)。
我的解决方案是添加DrawerListener,它告诉我的 PreferenceFragment 什么时候应该失效。
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
final MainOptionsFragment fragment = (MainOptionsFragment) getFragmentManager().
findFragmentById(R.id.fragment_drawer);
mDrawerOptions = new DrawerLayout.SimpleDrawerListener() {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
fragment.onOpened();
}
};
mDrawerLayout.addDrawerListener(mDrawerOptions);
另一种解决方案是覆盖onPrepareDialogBuilder方法,该方法将在准备对话框之前设置条目。
class CustomListPreference extends ListPreference {
@Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
// TODO setEntries
super.onPrepareDialogBuilder(builder);
}
}