Sliding Menu Demos 浅析:Changing Fragments

Changing Fragments:

360手机助手截图0807_12_12_04     360手机助手截图0807_13_47_01    360手机助手截图0807_13_47_02    360手机助手截图0807_13_48_01


     public class FragmentChangeActivity extends BaseActivity

FragmentChangeActivity继承了BaseActivity,同样的需要在oncreate中定义Above View。

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // set the Above View
        if (savedInstanceState != null)
            mContent = getSupportFragmentManager().getFragment(savedInstanceState, "mContent");
        if (mContent == null)
            mContent = new ColorFragment(R.color.red);    
        
        // set the Above View
        setContentView(R.layout.content_frame);
        getSupportFragmentManager()
        .beginTransaction()
        .replace(R.id.content_frame, mContent)
        .commit();
        
        // set the Behind View
        setBehindContentView(R.layout.menu_frame);
        getSupportFragmentManager()
        .beginTransaction()
        .replace(R.id.menu_frame, new ColorMenuFragment())
        .commit();
        
        // customize the SlidingMenu
        getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
    }

ColorFragment 类前面有介绍,一个设置背景色的Fragment。

ColorMenuFragment类前面同样有介绍:

      public class ColorMenuFragment extends ListFragment

当用户点击左边的menu时,它会调用FragmentChangeActivity里面的switchContent来替换Fragment。

@Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        getSupportFragmentManager().putFragment(outState, "mContent", mContent);
    }
    
    public void switchContent(Fragment fragment) {
        mContent = fragment;
        getSupportFragmentManager()
        .beginTransaction()
        .replace(R.id.content_frame, fragment)
        .commit();
        getSlidingMenu().showContent();
    }

onSaveInstanceState方法保存了要销毁之前的页面状态,当页面被用户切出还未销毁的时候,用户再次打开页面会读取存储的状态。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值