Skip to content

Instantly share code, notes, and snippets.

@r17171709
Last active September 7, 2015 09:25
Show Gist options
  • Save r17171709/7c6279ce0ee057ad3cb7 to your computer and use it in GitHub Desktop.
Save r17171709/7c6279ce0ee057ad3cb7 to your computer and use it in GitHub Desktop.
//fragment页签使用方法
//初始化时候将缓存的所有fragment全部隐藏
List<Fragment> fragments=getSupportFragmentManager().getFragments();
if(fragments!=null) {
for(int i=0;i<fragments.size();i++) {
getSupportFragmentManager().beginTransaction().hide(fragments.get(i)).commit();
}
}
//加载显示相应的fragment
if(currentFragmentTitle.equals("new")) {
switchFragment("new", 0);
}
else if(currentFragmentTitle.equals("locate")) {
switchFragment("locate", 1);
}
else if(currentFragmentTitle.equals("user")) {
switchFragment("user", 2);
}
//切换fragment页签方法
private void switchFragment(String title, int position) {
FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
if(currentFragment!=null) {
transaction.hide(currentFragment);
}
Fragment toFragment=getSupportFragmentManager().findFragmentByTag(title);
if(toFragment==null) {
if(position==0) {
toFragment=new HomepageFragment();
}
else if(position==1) {
toFragment=new GoShopingFragment();
}
else if(position==2) {
toFragment=new UserCenterFragment();
}
transaction.add(R.id.main_content, toFragment, title);
}
else {
transaction.show(toFragment);
}
transaction.commit();
currentFragment=toFragment;
currentFragmentTitle=title;
tabbar_new.setImageResource(R.drawable.btn_bottom_new);
tabbar_locate.setImageResource(R.drawable.btn_bottom_locate);
tabbar_user.setImageResource(R.drawable.btn_bottom_user);
if(position==0) {
tabbar_new.setImageResource(R.drawable.btn_bottom_new_active);
}
else if(position==1) {
tabbar_locate.setImageResource(R.drawable.btn_bottom_locate_active);
}
else if(position==2) {
tabbar_user.setImageResource(R.drawable.btn_bottom_user_active);
}
}
//保存可能产生的变量
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
outState.putString("currentFragmentTitle", currentFragmentTitle);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment