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

cho*_*boy 12 java navigation android android-fragments

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

我已经检查了这些帖子,但只是混淆了 Q1, Q2

有人可以解释从这个导航抽屉项目开始基本活动需要什么?我是否需要onClick在代码中指定的方法实现?这又与Intent有什么关系?

这是我的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);
}
Run Code Online (Sandbox Code Playgroud)

这是第二个活动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);
    }
}
Run Code Online (Sandbox Code Playgroud)

所有输入非常感谢谢谢!

Geo*_*gan 12

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

比如说,您希望Playboardnavigation_item_1选择时启动活动.

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

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