如何使导航抽屉打开活动?

时间:2020-01-23 16:33:42

标签: android android-activity navigation-drawer

在线上有很多关于如何将片段与导航抽屉一起使用的教程,但是我想知道如何使用它来打开导航抽屉。就像您选择导航抽屉的任何单个选项一样,它将带您进入另一个活动,而该活动与该活动中的导航抽屉的选项相同。

MainActivity.java

package com.example.fyptrial3;

import android.content.Intent;
import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.google.android.material.navigation.NavigationView;

import androidx.drawerlayout.widget.DrawerLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.view.Menu;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private DrawerLayout drawer;

    EditText etUsername, etPassword;


    public void OnLogin(View view) {
        String username = etUsername.getText().toString();
        String password = etPassword.getText().toString();
        String type = "login";

        BackgroundWorker backgroundWorker = new BackgroundWorker(this);
        backgroundWorker.execute(type, username, password);
    }

    private AppBarConfiguration mAppBarConfiguration;


    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        etUsername = (EditText) findViewById(R.id.etUsername);
        etPassword = (EditText) findViewById(R.id.etPassword);


        Toolbar toolbar = findViewById(R.id.toolbar);

        setSupportActionBar(toolbar);




        final DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_map,
                R.id.nav_news, R.id.nav_share, R.id.nav_login)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);

        navigationView.bringToFront();

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

                switch (menuItem.getItemId()) {
                    case R.id.nav_home:
                        Toast.makeText(getApplicationContext(), "Home is selected", Toast.LENGTH_LONG).show();
                        break;

                    case R.id.nav_gallery:
                        Toast.makeText(getApplicationContext(), "Members are selected",Toast.LENGTH_LONG).show();
                       Intent memberstart=new Intent(MainActivity.this,GalleryActivity.class);
                        startActivity(memberstart);
                        break;

                    case R.id.nav_map:
                        Toast.makeText(getApplicationContext(), "Slideshow is selected", Toast.LENGTH_LONG).show();
                        break;

                    case R.id.nav_share:
                        Toast.makeText(getApplicationContext(), "Share is selected", Toast.LENGTH_LONG).show();
                        //Intent sharestart=new Intent(MainActivity.this,this.class);
                       // startActivity(sharestart);
                        break;
                }

                drawer.closeDrawers();

                return false;
            }
        });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    //youtube.com/watch?v=zwabHRv2taA

    //In order to change it to Irish, I used this button to show that when it is clicked
    //the version of the Navigation Drawer as Gaeilge will pop up

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() ==R.id.main_settings) {
            Intent gaeilgemainstart=new Intent(MainActivity.this,MainActivityGaeilge.class);
            startActivity(gaeilgemainstart);

        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }

}

2 个答案:

答案 0 :(得分:1)

在您的 mobile_navigation.xml 中添加以下代码。

    <activity
    android:id="@+id/nav_custom"
    android:name="net.larntech.nav.CustomActivity"
    android:label="@string/menu_custom"
    tools:layout="@layout/activity_custom"
    />

这是一个演示相同内容的视频教程

https://github.com/yourdirectory/link.git

答案 1 :(得分:0)

您可以通过将NavigationView添加到您的活动XML中来添加

  <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:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

并将其添加到您的活动中

public class YourActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_your_activity);
        setTheme(R.style.full_screen);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);

        toggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.black));
        toggle.setHomeAsUpIndicator(R.drawable.side_menu);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        int id = menuItem.getItemId();
        switch (id) {
            case R.id.home:
                Intent intent = new Intent(HomeActivity.this, HomeActivity.class);
                startActivity(intent);
                break;
            case R.id.active_order:
                Intent active = new Intent(HomeActivity.this, ActiveOrderActivity.class);
                startActivity(active);
                break;
                case R.id.history_order:
                Intent history = new Intent(HomeActivity.this, HistoryOrderActivity.class);
                startActivity(history);
                break;
            case R.id.profile:
                Intent profile = new Intent(HomeActivity.this, ProfileActivity.class);
                startActivity(profile);
                break;
            case R.id.chat:
                Intent chat = new Intent(HomeActivity.this, ChatActivity.class);
                startActivity(chat);
                break;
            case R.id.logout:
                showPopup();
                break;
        }
        return true;
    }
 }

我希望这会对您有所帮助