我有一个带有操作栏的应用程序。它有一个“主要”片段和一个“首选项”片段。
final Main main = new Main();
final Preferences preferences = new Preferences();
drawer.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Fragment fragment= null;
FragmentManager fragmentManager = getSupportFragmentManager();
switch(position) {
case 0:
fragment = main;
break;
case 1:
fragment = preferences;
break;
}
fragmentManager.beginTransaction()
.replace(R.id.fragment1, fragment)
.commit();
drawerLayout.closeDrawers();
}
});
“Preferences”片段有一个 seekBar,它使用 SharedPreferences 中的值进行更新。这是在“首选项”片段中进行此操作的相关代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
container.removeAllViews();
View view = inflater.inflate(R.layout.preferences, container, false);
fontSizeBar = (SeekBar) view.findViewById(R.id.fontSizeBar);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(Preferences.this.getActivity().getBaseContext());
final int fontSize = preferences.getInt("fontSize", 10);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
fontSizeBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
Log.v("Progress changed", "Font size: " + progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Log.v("Start touching", "Font size: " + seekBar.getProgress());
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
Log.v("Stop touching", "Font size: " + seekBar.getProgress());
}
});
fontSizeBar.setMax(10);
fontSizeBar.setProgress(fontSize);
}
});
// Inflate the layout for this fragment
return inflater.inflate(R.layout.preferences, container, false);
}
调用 onProgressChanged() 方法,因为我可以在 LogCat 中看到输出“Progress changed”,“Font size: 10”,但是 SeekBar 没有视觉更新。此外,SeekBar 不响应 onStartTrackingTouch() 和 onStopTrackingTouch() 方法。
我将不胜感激任何可以阐明该问题的信息。