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方法保存了要销毁之前的页面状态,当页面被用户切出还未销毁的时候,用户再次打开页面会读取存储的状态。

posted @ 2015-08-07 13:56  黑泡man  阅读(125)  评论(0编辑  收藏  举报