滑动菜单以触摸和从右向左滑动关闭

时间:2013-02-22 07:02:13

标签: android actionbarsherlock slidingmenu

我从SlideMenu开始使用滑动菜单。我尝试了一个示例,但效果很好但是当显示菜单时,触摸右侧窗格或从左向右滑动关闭滑动菜单。

我需要滑动菜单仅在从右向左滑动时关闭或者可以在触摸时关闭。我不想从左到右滑动关闭菜单。如何明确处理上述3个案例?

public class MainActivity extends SherlockFragmentActivity {

SlidingMenu menu;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTheme(com.actionbarsherlock.R.style.Theme_Sherlock_Light);
    setContentView(R.layout.activity_main);
    menu = new SlidingMenu(this);
    menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
    menu.setMode(SlidingMenu.LEFT);
    menu.setShadowWidth(5);
    menu.setFadeDegree(0.0f);
    menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
    menu.setBehindWidth(200);
    menu.setMenu(R.layout.menulayout);
    ActionBar actionBar = getSupportActionBar();
    actionBar.setHomeButtonEnabled(true);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    getSupportMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        menu.toggle();
    }

    return true;
}
}

1 个答案:

答案 0 :(得分:0)

您的错误已开启:

public boolean onOptionsItemSelected(MenuItem item) 
{       
    switch (item.getItemId()) 
    {
        case android.R.id.home:
            SlidingMenu menu = getSlidingMenu();
            menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
            menu.toggle();
            return true;
    }

    return super.onOptionsItemSelected(item);
}

也可以查看这个答案:

Strange offset behavior on slidingmenu