我在活动中有一个导航抽屉和查看寻呼机。 问题是屏幕重叠。当我单击导航抽屉中的其他项目时,它会切换到相应的屏幕,但视图寻呼机保持不变。在视图分页器getItem()中,我将使用Fragment.newInstance(int index)调用片段。我尝试在视图分页器中更改getItem()方法以使用与导航抽屉相同的代码并替换当前片段,但是这给了我“java.lang.IllegalStateException:无法更改片段的容器ID”。我试图通过删除片段并提交更改来删除项目的ID。它没有修复错误。 任何建议或帮助将不胜感激。
public class MainActivity extends FragmentActivity {
private String[] titles;
private ListView drawerList;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private int currentPosition = 0;
private ShareActionProvider shareActionProvider;`
MyPagerAdapter myPagerAdapter;
ViewPager pager;
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//code to run when the item gets clicked
selectItem(position);
}
}
;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
titles = getResources().getStringArray(R.array.options);
drawerList = (ListView) findViewById(R.id.drawer);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
myPagerAdapter = new MyPagerAdapter(this.getSupportFragmentManager());
//find view pager
pager = (ViewPager) findViewById(R.id.viewPager);
//set view pager adapter
pager.setAdapter(myPagerAdapter);
//item user clicks on is highlighted
//with simple_list_item_activated_1
drawerList.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_activated_1, titles));
drawerList.setOnItemClickListener(new DrawerItemClickListener());
//if mainactivity is newly created,
//use selectItem() to display HomeFragment
if (savedInstanceState != null) {
currentPosition = savedInstanceState.getInt("position");
setActionBarTitle(currentPosition);
} else {
selectItem(0);
}
//create the actionbarToggle
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
R.string.open_drawer, R.string.close_drawer) {
//called when a drawer has settleed in a completely closed state
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
invalidateOptionsMenu();
}
};
drawerLayout.setDrawerListener(drawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
getSupportFragmentManager().addOnBackStackChangedListener(
new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
FragmentManager fragMan = getSupportFragmentManager();
Fragment fragment = fragMan.findFragmentById(R.id.content_frame);
if (fragment instanceof HomeFragment) {
currentPosition = 0;
}
if (fragment instanceof EducationFragment) {
currentPosition = 1;
}
if (fragment instanceof TechnicalSkillsFragment) {
currentPosition = 2;
}
if (fragment instanceof WorkExpFragment) {
currentPosition = 3;
}
setActionBarTitle(currentPosition);
drawerList.setItemChecked(currentPosition, true);
}
}
);
}
private Fragment selectItem(int position) {
//currentPosition for savedInstanceState
currentPosition = position;
Fragment fragment = null;
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft = fragmentManager.beginTransaction();
if(fragment !=null){
ft.remove(fragment).commit();
fragmentManager.executePendingTransactions();
}
switch (position) {
case 1:
fragment = new EducationFragment();
break;
case 2:
fragment = new TechnicalSkillsFragment();
break;
case 3:
fragment = new WorkExpFragment();
break;
default:
fragment = new HomeFragment();
break;
}
ft.replace(R.id.content_frame, fragment);
ft.addToBackStack(null);
ft.commit();
//set title and close the drawer
if (drawerLayout.isDrawerOpen(drawerList)) {
setActionBarTitle(position);
drawerLayout.closeDrawer(drawerList);
}
return fragment;
}
public class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public android.support.v4.app.Fragment getItem(int position) {
return selectItem(position);
}
@Override
public int getCount() {
return 4;
}