滑动菜单锁定上视图上的触摸事件

人气:990 发布:2022-10-16 标签: android menu android-viewpager slidingdrawer

问题描述

我正在尝试在我的应用程序中使用 滑动菜单.在我的 Sony Xperia S 上它工作得非常好,但是当我尝试在 HTC Desire HD 上启动应用程序时,菜单通过手势完美打开,但其他触摸事件被阻止并且顶部视图(ViewPager,滑动菜单是后面)没有滚动.

I'm trying to use Sliding Menu in my application. On my Sony Xperia S it works very nice, but when I try to launch app on HTC Desire HD, menu opens perfect by gesture, but other touch events are blocked and top view (ViewPager, sliding menu is behind it) is not scrolling.

有人知道如何解决这个问题吗?

Does anybody know how to fix this?

给出答案可能会有所帮助(这就是我使用菜单的方式):

May be it will be helpful to give an answer (This is how I'm using menu):

private void initSlidingMenu()  
{       
    mSlidingMenu = new SlidingMenu(getApplicationContext());

    mSlidingMenu.setMode(SlidingMenu.LEFT);
    mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
    mSlidingMenu.setShadowWidthRes(R.dimen.default_shadow_width);
    mSlidingMenu.setShadowDrawable(R.drawable.defaultshadow);
    mSlidingMenu.setBehindOffsetRes(R.dimen.default_behind_offset);
    mSlidingMenu.setFadeDegree(0.35f);
    mSlidingMenu.setMenu(firstPage);
    mSlidingMenu.attachToActivity(this,SlidingMenu.SLIDING_CONTENT);    
}

onPageSelected()中,我禁用或启用菜单,所以菜单只出现在左页:

In onPageSelected(), I disable menu or enable it, so menu appears only at left page:

@Override public void onPageSelected(int arg0)
{
    ActivityCompat.invalidateOptionsMenu(this);

    if (arg0 == Utils.DEFAULT_PAGE)

        mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

    else

        mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
}

推荐答案

我刚刚在运行 ICS 的三星 Galaxy S2 上遇到了同样的问题,但在运行 Gingerbread 的 HTC Desire 上却没有.

I just had the same problem with the Sliding menu working fine on my Samsung Galaxy S2 running ICS but not on my HTC Desire running Gingerbread.

我使用与您相同的方法来实现滑动菜单,但我发现另一种实现解决了问题.

I used the same method as you to implement the sliding menu but I found that another implementation solved the problem.

而不是手动将菜单附加到您的活动:

Instead of manually attaching the menu to your activity :

mSlidingMenu.attachToActivity(this,SlidingMenu.SLIDING_CONTENT);   

我选择通过 SlidingMenu 库中的一项滑动活动来扩展我的活动.就我而言,我的 Activity 一开始是在扩展 FragmentActivity 但我将其替换为 SlidingFragmentActivity

I chose to extend my Activity by one of the Sliding activities from the SlidingMenu library. In my case, my Activity was extending FragmentActivity at first but I replaced it by SlidingFragmentActivity

那么你应该在 onCreate 方法中设置 behindView 否则你的 Activity 会崩溃.

Then you should set the behindView inside the onCreate method or your activity will crash.

setBehindContentView(R.layout.slidingmenu); //Replace with the layout of your menu

它应该可以解决问题.

这是我的 Activity 的示例代码:

Here is a sample code of my Activity :

public class MainActivity extends SlidingFragmentActivity {

SlidingMenu menu;

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

    // Main view layout for the activity
    setContentView(R.layout.activity_listing); 

    // set the Behind View : REQUIRED (replace with your menu's layout)
    setBehindContentView(R.layout.slidingmenu);

    menu = getSlidingMenu();

    //Now you can customize your sliding menu if you want
    menu.setMode(SlidingMenu.LEFT);
    menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
    menu.setShadowWidthRes(R.dimen.shadow_width);
    menu.setShadowDrawable(R.drawable.slidingmenu_shadow);
    menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
    menu.setFadeDegree(0.35f);

您会注意到,使用此解决方案您不必调用

You will note that with this solution you don't have to call

mSlidingMenu.setMenu(firstPage);
mSlidingMenu.attachToActivity(this,SlidingMenu.SLIDING_CONTENT);

我还没有分析 SlidingMenu 库的所有源代码,但可能当前的 attachToActivity 方法在某些情况下存在错误,如果我们使用由提供的特殊活动,这些错误不会出现图书馆.

I haven't analyzed all the source code of the SlidingMenu library but maybe the current attachToActivity method is buggy in some cases and those bugs don't appear if we use the special activities provided by the library.

好的,适合使用 SlidingMenu 库和 ActionBarSherlock (ABS) 的人.首先,您必须确保在 SlidingMenu 库中引用了 ABS.这样,您就可以通过 ABS 扩展 SlidingMenu 提供的活动类.

EDIT : Ok for those who are working with the SlidingMenu library AND ActionBarSherlock (ABS). First you have to make sure that ABS is referenced in the SlidingMenu lib. That way, you'll be able to extend the activities classes provided by SlidingMenu by the ABS ones.

如果我想使用带有 ABS 和滑动菜单的 FragmentActivity 示例:你必须在滑动菜单库中改变

Example if I want to use an FragmentActivity with both ABS and Sliding menu : you have to change in the sliding menu library

class SlidingFragmentActivity extends FragmentActivity implements SlidingActivityBase

class SlidingFragmentActivity extends SherlockFragmentActivity implements SlidingActivityBase

然后在您的应用程序中,正常使用 SlidingFragmentActivity.

And then in you application, use the SlidingFragmentActivity normally.

这是我应用的代码,和我发布的一样,但支持 ABS:

Here is the code of my app, it is like the one I posted but with ABS support :

public class MainActivity extends SlidingFragmentActivity {

ActionBar mActionBar;
SlidingMenu menu;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_listing); //Layout of the above view

    /*
     * ABS initialization
     */
    mActionBar = getSupportActionBar();
    mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    mActionBar.setDisplayHomeAsUpEnabled(true);

    /*
     * Sliding menu initialization
     */
    menu = getSlidingMenu();
    menu.setMode(SlidingMenu.LEFT);
    menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
    menu.setShadowWidthRes(R.dimen.shadow_width);
    menu.setShadowDrawable(R.drawable.slidingmenu_shadow);
    menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
    menu.setFadeDegree(0.35f);

            /* This line initializes the way the sliding menu is working
            if you set the boolean to true, the ActionBar will slide along with the content.
            if you set to false, only the content will slide and the ActionBar will not move */
    setSlidingActionBarEnabled(true);

    // set the Behind View
    setBehindContentView(R.layout.slidingmenu); //Menu view

447