我创建了一个导航抽屉和一个片段我试图从导航抽屉打开那个片段,但没有任何帮助请发生..
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main by replacing fragments
Fragment fragment;
switch(position){
case 0: //search//todo
break;
case 1: //stats
break;
case 2: //my account //todo
break;
case 3: //settings //todo
break;
case 4: //location //todo
fragment = getFragmentManager().findFragmentByTag(LocationFragment.TAG);
if (fragment == null) {
fragment = new LocationFragment();
}
getFragmentManager().beginTransaction().replace(R.id.container, fragment, LocationFragment.TAG).commit();
break;
}
答案 0 :(得分:1)
我更改了一些代码,这应该可行。如果出现任何问题,请随时发表评论。您找不到您的位置片段,因为您没有setTag
。
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main by replacing fragments
Fragment fragment;
String fragmentName;
switch(position) {
case 0: //search//todo
fragment = new SearchFragment();
fragmentName = "SearchFragment";
break;
case 1: //stats
fragment = new StatFragment();
fragmentName = "StatFragment";
break;
case 2: //my account //todo
fragment = new MyAccountFragment();
fragmentName = "MyAccountFragment";
break;
case 3: //settings //todo
fragment = new SettingsFragment();
fragmentName = "SettingsFragment";
break;
case 4: //location //todo
fragment = new LocationFragment();
fragmentName = "LocationFragment";
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.container, fragment, fragmentName).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
mDrawerLayout.closeDrawer(mDrawerRelativeLayout);
} else {
mDrawerLayout.closeDrawer(mDrawerRelativeLayout);
}
}