简体   繁体   English

如何使用导航抽屉上的项目打开其他活动

[英]How to open other activities using items on the navigation drawer

I am creating a guide app, and I want other users to tap on an item on the navigation drawer, how can I make it open another activity?我正在创建一个指南应用程序,我希望其他用户点击导航抽屉上的一个项目,我怎样才能让它打开另一个活动? This is the MainApplication.java:这是 MainApplication.java:

 package net.agiann.paliachora_guide;

import android.app.Application;
import android.test.ApplicationTestCase;

    /**
     * <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
     */
    public class ApplicationTest extends ApplicationTestCase<Application> {
        public ApplicationTest() {
            super(Application.class);
        }
    }
 navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

            // This method will trigger on item Click of navigation menu
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {


                //Checking if the item is in checked state or not, if not make it in checked state
                if (menuItem.isChecked()) menuItem.setChecked(false);
                else menuItem.setChecked(true);

                //Closing drawer on item click
                drawerLayout.closeDrawers();

                //Check to see which item was being clicked and perform appropriate action
                switch (menuItem.getItemId()) {


                    //Replacing the main content with ContentFragment Which is our Inbox View;
                    case R.id.drawer_home:

                        Intent intent=new Intent(HomeActivity.this, HomeActivity.class);
                        startActivity(intent);

                        finish();
                        return true;

                    // For rest of the options we just show a toast on click


                    default:
                        Toast.makeText(getApplicationContext(), "Somethings Wrong", Toast.LENGTH_SHORT).show();
                        return true;

                }
            }
        });

try this:尝试这个:

implement your activity NavigationView.OnNavigationItemSelectedListener实施您的活动NavigationView.OnNavigationItemSelectedListener

public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int resultCode = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(this);
    int id = item.getItemId();

    if (id == R.id.nav_home) {
        Intent intent=new Intent(this,HomeActivity.class);
        startActivity(intent);
    }else if(second codition){

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                mDrawerLayout.closeDrawers();
                switch (menuItem.getItemId()) {
                    case R.id.nav_item_Animation:
                      Intent intent=new Intent(this,MainActivity.class);
    startActivity(intent);
                        break;
                    case R.id.nav_item_loginScreen:
                       Intent intent=new Intent(this,LoginActivity.class);
    startActivity(intent);
                        break;
}
                return false;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM