从导航抽屉菜单中打开一个新活动

时间:2019-01-27 15:42:15

标签: android android-activity menu navigation drawer

当我选择一个项目时,我正在尝试从导航抽屉菜单中打开一个新活动。不幸的是,当我单击项目菜单时,没有任何反应,

我从这里开始关注本教程:https://developer.android.com/training/implementing-navigation/nav-drawer#OpenClose

除了我尝试通过onNavigationItemSelected方法向抽屉菜单按钮添加功能的那一部分之外,其他所有东西都工作正常。

我也尝试过在onOptionsItemSelected()方法上添加相同的功能,但是没有用。

这是我的HomeActivity.java代码

private DrawerLayout mDrawerLayout;

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

    ActionBar actionbar = getSupportActionBar();
    actionbar.setDisplayHomeAsUpEnabled(true);
    actionbar.setHomeAsUpIndicator(R.drawable.ic_menu);

    mDrawerLayout = findViewById(R.id.drawer_layout);

    spinnerCategory = (Spinner) findViewById(R.id.spinnerCategoryAffiliation);
    listView = (ListView) findViewById(R.id.listViewProducts);
    productList = new ArrayList<>();
    categoryList = new ArrayList<>();


    NavigationView navigationView = findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(
            new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem menuItem) {
                    // set item as selected to persist highlight
                    menuItem.setChecked(true);
                    // close drawer when item is tapped
                    mDrawerLayout.closeDrawers();

                    int id = menuItem.getItemId();

                    // Add code here to update the UI based on the item selected
                    // For example, swap UI fragments here
                    if (id == R.id.openCartMenu){
                        Intent newAct = new Intent(HomeActivity.this, ShoppingCartActivity.class);
                        startActivity(newAct);
                    } else if (id == R.id.openProductsMenu){

                    } else if (id == R.id.openPersonalInfoMenu){

                    } else if (id == R.id.openOrdersMenu){

                    }

                    return true;
                }
            });

    mDrawerLayout.addDrawerListener(
            new DrawerLayout.DrawerListener() {
                @Override
                public void onDrawerSlide(View drawerView, float slideOffset) {
                    // Respond when the drawer's position changes
                }

                @Override
                public void onDrawerOpened(View drawerView) {
                    // Respond when the drawer is opened
                }

                @Override
                public void onDrawerClosed(View drawerView) {
                    // Respond when the drawer is closed
                }

                @Override
                public void onDrawerStateChanged(int newState) {
                    // Respond when the drawer motion state changes
                }
            }
    );

    readCategories();

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            mDrawerLayout.openDrawer(GravityCompat.START);
            return true;
    }
    return super.onOptionsItemSelected(item);
}

这是我的activity_home.xml的代码

<?xml version="1.0" encoding="utf-8"?>
<!-- Use DrawerLayout as root container for activity -->
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <!-- Layout to contain contents of main body of screen (drawer will slide over this) -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <!-- Container for contents of drawer - use NavigationView to make configuration easier -->
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:menu="@menu/drawer_view"
        app:headerLayout="@layout/nav_header_drawer"/>

    <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"
        tools:context="com.seeshop.app.HomeActivity">

        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:orientation="vertical"
            android:padding="16dp">

            <Spinner
                android:id="@+id/spinnerCategoryAffiliation"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="8dp" />
        </LinearLayout>

        <ListView
            android:id="@+id/listViewProducts"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/linearLayout" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/openCartButton"
            android:layout_width="69dp"
            android:layout_height="67dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:layout_gravity="end|bottom"
            android:layout_marginTop="471dp"
            android:layout_marginEnd="21dp"
            android:src="@drawable/carticon" />




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

这是我的cabinet_view.xml的代码

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/openCartMenu"
            android:icon="@drawable/ic_shopping_cart"
            android:title="Shopping cart" />
        <item
            android:id="@+id/openProductsMenu"
            android:icon="@drawable/ic_products"
            android:title="Products" />
        <item
            android:id="@+id/openPersonalInfoMenu"
            android:icon="@drawable/ic_personal_info"
            android:title="Personal info" />
        <item
            android:id="@+id/openOrdersMenu"
            android:icon="@drawable/ic_orders"
            android:title="My orders" />
    </group>
</menu>

非常感谢您的帮助。

0 个答案:

没有答案