jfeinstein10 / SlidingMenu

An Android library that allows you to easily create applications with slide-in menus. You may use it in your Android apps provided that you cite this project and include the license in your app. Thanks!
Apache License 2.0
11.08k stars 5.05k forks source link

Strange behavior in implementing LEFT_RIGHT sliding menu #533

Open alawibh opened 11 years ago

alawibh commented 11 years ago

Dears,

I'm new to Android, and try to use SlidingMenu in my project. I want to create an application with two sliding menus same as Facebook.

With these steps I had crated my application:

  1. Download and add SlidingMenu library into my work-space.
  2. Download and add actionbarsherlock library to my work-space.
  3. In SlidingMenu library I add actionbarsherlock as a reference library.
  4. In SlidingMenu library, I extend SlidingActivity.java with actionbarsherlock: public class SlidingActivity extends SherlockActivity implements SlidingActivityBase
  5. In my application, I add reference to SlidingMenu library.
  6. In my application, I extend my main activity to use SlidingActivity : public class MainMenu extends SlidingActivity

My main activity code:

import android.app.ActionBar;
import android.os.Bundle;
import android.view.KeyEvent;

import com.actionbarsherlock.view.MenuInflater;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingActivity;

public class MainMenu extends SlidingActivity {
    SlidingMenu  menu;

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

        if (enableHomeIconActionBack() || enableHomeIconActionSlidingMenu()) {
            ActionBar actionBar = getActionBar();
            if (actionBar != null){
                actionBar.setDisplayHomeAsUpEnabled(true);
            }
        }

        setContentView(R.layout.activity_main_menu);
        setBehindContentView(R.layout.activity_main_menu);

        menu = new SlidingMenu(this);
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        menu.setMode(SlidingMenu.LEFT_RIGHT);
        menu.setShadowWidthRes(R.dimen.sliding_menu_shadow_width);
        menu.setShadowDrawable(R.drawable.shadow);
        menu.setBehindOffsetRes(R.dimen.sliding_menu_offset);
        menu.setFadeDegree(0.35f);
        menu.attachToActivity(this, SlidingMenu.SLIDING_WINDOW);
        menu.setMenu(R.layout.sliding_menu_frame);

        menu.setSecondaryMenu(R.layout.vehicles_tree);
        menu.setSecondaryShadowDrawable(R.drawable.shadow);
    }

    @Override
    public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            if (enableHomeIconActionSlidingMenu()
                    && menu != null) {
                menu.toggle();
            } else if (enableHomeIconActionBack()) {
                onCustomBackPressed();
            }
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }   

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_BACK:
            onCustomBackPressed();
            return true;
        default:
            return super.onKeyDown(keyCode, event);
        }
    }

    // If sliding menu is showing, we need to hide it on the first back button
    // press.
    private void onCustomBackPressed() {
        if (menu != null
                && menu.isMenuShowing()) {
            menu.toggle();
        } else {
            this.onBackPressed();
        }
    }

    /**
     * Sets activity home icon to have up icon and on press act as device back
     * button press.
     * 
     * @return Activation state.
     */
    public boolean enableHomeIconActionBack() {
        return true;
    }

    /**
     * Sets activity home icon to be as a sliding menu invoke icon and on press
     * call toggle command for the sliding menu.
     * 
     * @return Activation state.
     */
    public boolean enableHomeIconActionSlidingMenu() {
        return true;
    }   
}

Now, menu.setMenu(R.layout.sliding_menu_frame); will show:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFF00FF" >
</RelativeLayout>

And menu.setSecondaryMenu(R.layout.vehicles_tree); will show:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:background="#FF0F00FF">
</RelativeLayout>

The following snapshots describe the strange behavior:

Alt Text

  1. Main activity.
  2. When you swipe right on the screen.
  3. When you swipe left on the screen.
  4. When you swipe right from left screen edge. This activity covers the entire screen and it is showing the activity attached to setBehindContentView(R.layout.activity_main_menu);, I noticed that by changing the attached layout.

I don't want the forth screen, what I'm doing wrong?

Regards,, Ali Alaswad

zmckenzie commented 11 years ago

When you extend from SlidingActivity there is no need to add another menu. Whats happening here is that there are two implementations on the menu. To fix the issue change

menu = new SlidingMenu(this);  

to

menu = getSlidingMenu();