【问题标题】:How to open Activity from Android's built-in navigation drawer如何从 Android 的内置导航抽屉中打开 Activity
【发布时间】:2020-04-15 11:29:21
【问题描述】:

我见过很多人遇到同样的问题,但是当我尝试他们的解决方案时,它仍然无法从导航抽屉打开活动。

我的 MainActivity 的代码

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

//region Properties
private AppBarConfiguration mAppBarConfiguration;
ImageButton settings_btn;
//endregion

@Override
protected void onStart() {
    super.onStart();
    ProductDatabase productDb = new ProductDatabase();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    settings_btn = findViewById(R.id.btn_settings);
    settings_btn.setOnClickListener(v ->
            startActivity(new Intent(MainActivity.this, SettingsActivity.class)));

    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(view -> Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
            .setAction("Action", null).show());

    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_login, R.id.nav_treatments, R.id.nav_product, R.id.nav_about, R.id.nav_contactUs)
            .setDrawerLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);
}

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

@Override
public void onBackPressed() {
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.nav_booking) {
        startActivity(new Intent(this, BookingActivity.class));
    } else if (id == R.id.nav_home) {

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

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

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

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

    }

    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
    }
}

当我运行此代码并单击“预订”选项卡时。什么都没发生。 Booking 之前是一个 Fragment,但现在我只想让它打开一个 Activity,而所有其他选项都打开 Fragment。 提前致谢

【问题讨论】:

  • 你确定你的活动调用按钮有 id nav_booking,你测试过你的代码吗?

标签: java android android-activity navigation-drawer


【解决方案1】:
navigationView.setNavigationItemSelectedListener(this)

我没有在您的代码中看到这行代码。显然你没有设置监听器

【讨论】:

  • 如果我的回答对您有帮助,请单击答案左侧的复选标记将其标记为正确。
  • 在 5 分钟内无法完成
  • 我在添加您的代码时遇到了另一个问题。添加代码后,所有其他选项均无效。所以只有 BookingActivity 有效,其他片段无效
  • 所以在我的 onNavigationItemSelected 中,我需要在获取它们的 id 时启动其他片段。但是我如何从一个活动开始一个片段?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-05
  • 1970-01-01
  • 2013-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多