Fragment的初步用法
protected void replaceFragment(Fragment fragment , boolean init) { FragmentManager fragmentManager = this.getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); if(init) { fragmentTransaction.add(R.id.main_root, fragment); } else { fragmentTransaction.setCustomAnimations(R.anim.right_in , R.anim.left_out , R.anim.left_in , R.anim.right_out);//添加切换进入和back键返回的动画 fragmentTransaction.replace(R.id.main_root, fragment);//切换片段 fragmentTransaction.addToBackStack(null);//并且加入stack中,以便返回 } fragmentTransaction.commit();//提交 } private void addFragment() { FragmentManager fragmentManager = this.getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); GalleryFlowFragment fragment = new GalleryFlowFragment(); fragmentTransaction.add(R.id.main_root, fragment);//添加片段 fragmentTransaction.commit(); } public void popFragment() { FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.popBackStack();// 返回前一个片段 }
相关链接:
http://leybreeze.com/blog/?p=902