我有一个带操作栏的应用。它有一个“Main”片段和一个“Preferences”片段。
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();
}
});
“首选项”片段有一个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()方法。
我将不胜感激任何可以解释问题的信息。
答案 0 :(得分:0)
你正在做正确的事情,夸大观点并设置逻辑,但最后你要做到这一点:
return inflater.inflate(R.layout.preferences, container, false);
将返回新膨胀的布局(因此您放弃之前完成的所有内容)。相反,您需要返回之前虚增的view
:
return view;