Java 在导航抽屉项目单击上启动新活动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36463696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:54:29  来源:igfitidea点击:

Starting a new activity on navigation drawer item click

javaandroidandroid-fragmentsnavigation

提问by choloboy

I know this is a question frequently asked, but after reading the many questions and solutions on stack overflow I am confused. I am confused with regards to Fragmentsand what is required to start an activity from clicking an item in the navigation drawer.

我知道这是一个经常被问到的问题,但是在阅读了有关堆栈溢出的许多问题和解决方案后,我感到很困惑。我对Fragments单击导航抽屉中的项目以及启动活动所需的条件感到困惑。

I've checked these posts but only got confused Q1, Q2

我已经检查了这些帖子,但只对Q1Q2感到困惑

Could someone please explain what is required to start a basic activity from this navigation drawer item? Do I need an onClickmethod implemented where specified in the code? Also how does this relate to the Intent?

有人可以解释一下从这个导航抽屉项目开始基本活动需要什么吗?我需要onClick在代码中指定的地方实现一个方法吗?这与意图有何关系?

Here is my MainActivity.java

这是我的 MainActivity.java

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

DrawerLayout drawerLayout;
ActionBarDrawerToggle drawerToggle;
NavigationView navigation;

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

private void initInstances() {
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    drawerToggle = new ActionBarDrawerToggle(MainActivity.this, drawerLayout, R.string.hello_world, R.string.hello_world);
    drawerLayout.setDrawerListener(drawerToggle);

    navigation = (NavigationView) findViewById(R.id.navigation_view);
    navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            int id = menuItem.getItemId();
            switch (id) {
                case R.id.navigation_item_1:
                    //Do some thing here
                    // add navigation drawer item onclick method here
                    break;
                case R.id.navigation_item_2:
                    //Do some thing here
                    // add navigation drawer item onclick method here
                    break;
                case R.id.navigation_item_3:
                    //Do some thing here
                    // add navigation drawer item onclick method here
                    break;
                case R.id.navigation_item_4:
                    //Do some thing here
                    // add navigation drawer item onclick method here
                    break;
                case R.id.navigation_item_5:
                    //Do some thing here
                    // add navigation drawer item onclick method here
                    break;
            }
            return false;
        }
    });

}

@Override
public void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    drawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    drawerToggle.onConfigurationChanged(newConfig);
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (drawerToggle.onOptionsItemSelected(item))
        return true;

    // 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.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.string.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

And here is the second activity, Playboard.java, that simply loads a background image:

这是第二个活动 Playboard.java,它只加载背景图像:

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Playboard extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_playboard);
    }
}

All input greatly appreciated thanks!

非常感谢所有输入,谢谢!

采纳答案by George Mulligan

For each case statement you just need to specify which Activityyou want to start via an Intent.

对于每个 case 语句,您只需指定Activity要通过Intent.

Say for example you want to start the Playboardactivity when navigation_item_1is selected.

比如说你想Playboardnavigation_item_1被选中时开始活动。

You would add this code to that particular case.

您可以将此代码添加到该特定case.

case R.id.navigation_item_1:
    Intent i = new Intent(MainActivity.this, Playboard.class);
    startActivity(i);
    break;

回答by xtro

One word of warning: if you have any animations on the drawerbox, starting an activity from the main thread directly will cause the animation to end prematurely and look weird. To work around this issue you can do the following (code uses retrolambda for prettiness, but it's not necessary):

一个警告:如果抽屉盒上有任何动画,直接从主线程启动 Activity 会导致动画过早结束并看起来很奇怪。要解决此问题,您可以执行以下操作(代码使用 retrolambda 来美观,但这不是必需的):

Class<? extends Activity> activityClass = null;
switch (menuItem.getItemId()) {
  case R.id.navigation_item_1:
    activityClass = MainActivity.class;
    break;
 }

 final Class<?> finalActivityClass = activityClass;
 Executors.newSingleThreadExecutor().execute(() -> {
   Intent intent = new Intent(getApplicationContext(), finalActivityClass);
   startActivity(intent);
 });

 menuItem.setChecked(true);
 mDrawerLayout.closeDrawers();