0

我想使用两个Drawer Menu,其中一个在左侧,另一个在右侧。leftDrawer Menu就ok了,当item被点击后,操作就执行了。但在右边Drawer Menu,当点击项目时,操作不会!
我的 XML 代码:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tb="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/DrawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/background_color"
        tools:context=".Main_Page">

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/viewpagertab">

        </android.support.v4.view.ViewPager>

    </RelativeLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/main_drawer"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/header_menu_app"
        app:itemIconTint="@color/post_card_color"
        app:itemTextColor="@color/post_card_color"
        app:menu="@menu/menu_main__page" />

    <android.support.design.widget.NavigationView
        android:id="@+id/main_drawer2"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        app:headerLayout="@layout/header_drawer_main"
        app:itemIconTint="@color/post_card_color"
        app:itemTextColor="@color/post_card_color"
        app:menu="@menu/menu_main_profile" />

</android.support.v4.widget.DrawerLayout>

我的 JAVA 代码:

public class Main_Page extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

@Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main__page);

    // Left DraweLayout Open
        Drawer = (DrawerLayout) findViewById(R.id.DrawerLayout);
        mNaviView = (NavigationView) findViewById(R.id.main_drawer);
        mNaviView2 = (NavigationView) findViewById(R.id.main_drawer2);
        mNaviView.setNavigationItemSelectedListener(this);
        mDrawerToggle = new ActionBarDrawerToggle(this, Drawer, toolbar, R.string.openDrawer, R.string.closeDrawer) {

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                if (con.isOnline()) {
                    new get_menu_info().execute(public_username);
                }
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);

            }
        };
        Drawer.setDrawerListener(mDrawerToggle);
        mDrawerToggle.syncState();

    // Right DraweLayout Open
        ImageView profile_menu = (ImageView) toolbar.findViewById(R.id.right_tool);
        profile_menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Drawer.openDrawer(mNaviView2);
            }
        });
}

  @Override
    public boolean onNavigationItemSelected(MenuItem menuItem) {
        Intent intent;

        if (menuItem.getItemId() == R.id.menu_action_mySMS) {
            intent = new Intent(this, MySMS_Page.class);
            startActivity(intent);
            overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_top);
            finish();
            return true;
        }

        if (menuItem.getItemId() == R.id.menu_profile_action_favorite) {
            intent = new Intent(this, Fav_Page.class);
            startActivity(intent);
            overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_top);
            finish();
            return true;
        }

        if (menuItem.getItemId() == R.id.menu_action_category) {
            intent = new Intent(this, category_Page.class);
            startActivity(intent);
            overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
            finish();
            return true;
        }

        if (menuItem.getItemId() == R.id.menu_action_setting) {
            intent = new Intent(this, setting_Page.class);
            startActivity(intent);
            overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
            finish();
            return true;
        }

        if (menuItem.getItemId() == R.id.menu_action_about) {
            intent = new Intent(this, about_Page.class);
            startActivity(intent);
            overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
            finish();
            return true;
        }

        if (menuItem.getItemId() == R.id.menu_action_top) {
            intent = new Intent(this, top_Page.class);
            startActivity(intent);
            overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
            return true;
        }

        if (menuItem.getItemId() == R.id.menu_action_free_com) {
            intent = new Intent(this, freeCon_Page.class);
            startActivity(intent);
            overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
            return true;
        }
        return false;
    }
}

单击时请帮助我调用正确的菜单项。
tnx <3

4

2 回答 2

1

试试这个代码:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
     if (item.getItemId() == R.id.menuitemid) {
        if(mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
                        mDrawerLayout.closeDrawer(Gravity.LEFT); // set Gravity as per your need
                    }else{
                        mDrawerLayout.openDrawer(Gravity.LEFT); // set Gravity as per your need
                    }      

    }else if (item.getItemId()== R.id.action_settings) {
        return true;
    } 
    return super.onOptionsItemSelected(item);
}

希望它会奏效。

于 2015-10-31T07:56:42.073 回答
0

试试这个,

@Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.lnrLytMenuSlider:
                switchSliderLeft();
                break;
        }
}

调用这个方法,

public void switchSliderLeft()  {
        getSlidingMenu().showMenu();
        getSlidingMenu().setMode(SlidingMenu.LEFT);
}
于 2015-10-31T07:28:32.983 回答