Sliding Menu Demos 浅析:Sliding Title Bar 与 Sliding Content Only

卫宏硕
2023-12-01

Sliding Title Bar 与 Sliding Content Only两个demo实现了不同的功能,前者在用户滑动的时候,Actionbar也能跟随主Activity移动,后者则保持Actionbar位置不变。

Sliding Title Bar 代码:

 public class SlidingTitleBar extends BaseActivity {

    public SlidingTitleBar() {
        super(R.string.title_bar_slide);
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // set the Above View
        setContentView(R.layout.content_frame);
        getSupportFragmentManager()
        .beginTransaction()
        .replace(R.id.content_frame, new SampleListFragment())
        .commit();
        
        setSlidingActionBarEnabled(true);
    }
    
}

SlidingTitleBar 继承了 BaseActivity ,BaseActivity的代码如下:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setTitle(mTitleRes);

        // set the Behind View
        setBehindContentView(R.layout.menu_frame);
        if (savedInstanceState == null) {
            //开启事务
            FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
            mFrag = new SampleListFragment();
            t.replace(R.id.menu_frame, mFrag);//将SampleListFragment添加到FrameLayout布局中来
            t.commit();//提交事务
        } else {
            mFrag = (ListFragment)this.getSupportFragmentManager().findFragmentById(R.id.menu_frame);
        }

        // 配置Sliding Menu
        SlidingMenu sm = getSlidingMenu();
        sm.setShadowWidthRes(R.dimen.shadow_width);
        sm.setShadowDrawable(R.drawable.shadow);
        sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        sm.setFadeDegree(0.35f);
        sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

BaseActivity只是配置了隐藏的视图,而在SlidingTitleBar中配置了最上层的视图。Sliding Menu本质上使用的是类似Framelayout的布局加上复杂的动画。

其中:

setSlidingActionBarEnabled(true);

可以配置Actionbar是否会随内容滑动。

 类似资料: