【发布时间】:2015-02-10 21:40:57
【问题描述】:
我有五个片段。每个片段加载两种类型的列表视图(学生列表和员工列表)。用户必须从设置(共享首选项)中选择它在所有五个片段中看到的列表类型,无论是学生列表还是所有五个片段中的员工列表。效果很好。
现在我在导航抽屉中放置了一个文本(菜单项)。单击该文本后,(共享首选项应更改)现有列表应从学生列表切换为教职员工列表或教职员工列表切换为学生列表。
我尝试了某种方式,但在切换列表后我的片段没有重新加载
public void onNavigationDrawerItemSelected(int position, NavItem item){
Fragment fragment;
String switchs = item.getData();
if(switchs.equals("switch")){
SharedPreferences sharedPref= getApplicationContext().getSharedPreferences("switchview", 0);
String my_view = sharedPref.getString("view", "");
Log.v("INFO",switchs + my_view);
switch (my_view){
case "true" :
my_view = "false";
break;
case "false" :
my_view = "true";
break;
}
Log.v("INFO","myView" + my_view);
SharedPreferences.Editor editor= sharedPref.edit();
editor.putString("view", my_view);
editor.commit();
// Reload current fragment
Fragment frg = null;
frg = getSupportFragmentManager().findFragmentByTag("main");
Log.v("INFO","fragment" + frg);
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.container, frg,"main").commit();
}else{
try {
fragment = item.getFragment().newInstance();
if (fragment != null) {
//adding the data
Bundle bundle = new Bundle();
String extra = item.getData();
bundle.putString(DATA, extra);
fragment.setArguments(bundle);
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment,"main").commit();
setTitle(item.getText());
if (null != MainActivity.this.getSupportActionBar() && null != MainActivity.this.getSupportActionBar().getCustomView()){
MainActivity.this.getSupportActionBar().setDisplayOptions(
ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
}
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
帮帮我,谢谢。
【问题讨论】:
标签: android android-activity android-fragments