How to make SlidingMenu only slide content and not the ActionBar

微笑、不失礼 提交于 2019-12-13 02:09:45

问题


I am using SlidingMenu with ActionBarSherlock in my app and the way I have implemented it is by making a BaseActivity which extends SlidingFragmentActivity ( similar to the way they showed in the example ). Every other activity in my app extends this BaseActivity.

But currently, when I slide from the left in the App, the entire activity including the ActionBar slides. I only want the content to slide. So looking at the docs, I figured that adding this should do it: menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);

But I get an error for that line -

java.lang.IllegalStateException: This SlidingMenu appears to already be attached

Here's the onCreate function of the 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 MenuListFragment();
            t.replace(R.id.menu_frame, mFrag);
            t.commit();
        } else {
            mFrag = (ListFragment)this.getSupportFragmentManager().findFragmentById(R.id.menu_frame);
        }

        // customize the SlidingMenu
        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);

   // **** This following line causes the error  *****
        sm.attachToActivity(this, SlidingMenu.SLIDING_WINDOW);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

Should I be doing the attachToActivity somewhere else?


回答1:


I eventually solved it by removing this from the Base activity - sm.attachToActivity(this, SlidingMenu.SLIDING_WINDOW);

and just adding this to the onCreate of all the inherited Activities:

setSlidingActionBarEnabled(false);

I guess this is coming from a long line of inheritance (myActivity --> BaseActivity ---> SlidingFragmentActivity --> SherlockFragmentActivity)




回答2:


You used the command line:

SlidingMenu sm = getSlidingMenu();

and you didn't attach the your menu because your menu is already attached.

use the command:

sm.attachToActivity(this, SlidingMenu.SLIDING_WINDOW); 

if you create a new instance of SlidingMenu. Like a SlidingMenu menu = new SlidingMenu(this);



来源:https://stackoverflow.com/questions/18798477/how-to-make-slidingmenu-only-slide-content-and-not-the-actionbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!