问题
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);
}
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);
}
回答1:
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);
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
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
It should fix the problem.
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);
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.
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.
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
by
class SlidingFragmentActivity extends SherlockFragmentActivity implements SlidingActivityBase
And then in you application, use the SlidingFragmentActivity
normally.
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
来源:https://stackoverflow.com/questions/13895149/sliding-menu-locks-touch-event-on-upper-view