单击导航抽屉项上的新 activity
Starting a new activity on navigation drawer item click
我知道这是一个经常被问到的问题,但在阅读了关于堆栈溢出的许多问题和解决方案后,我感到困惑。我对 Fragments
以及通过单击导航抽屉中的项目启动 activity 需要什么感到困惑。
我查看了这些帖子,但只是感到困惑
,
有人可以解释从这个抽屉式导航项目开始基本 activity 需要什么吗?我是否需要在代码中指定的位置实施 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);
}
这是第二个 activity、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);
}
}
非常感谢所有输入,谢谢!
对于每个 case 语句,您只需通过 Intent
.
指定要从哪个 Activity
开始
例如,您希望在选择 navigation_item_1
时启动 Playboard
activity。
您可以将此代码添加到特定的 case
。
case R.id.navigation_item_1:
Intent i = new Intent(MainActivity.this, Playboard.class);
startActivity(i);
break;
一个警告:如果抽屉盒上有任何动画,直接从主线程启动 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();
而不是
NavigationUI.setupWithNavController(navigationView, navController);
这样做
navigationView.setNavigationItemSelectedListener(new
NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_SHORT).show();
return false;
}
}
);
我知道这是一个经常被问到的问题,但在阅读了关于堆栈溢出的许多问题和解决方案后,我感到困惑。我对 Fragments
以及通过单击导航抽屉中的项目启动 activity 需要什么感到困惑。
我查看了这些帖子,但只是感到困惑
有人可以解释从这个抽屉式导航项目开始基本 activity 需要什么吗?我是否需要在代码中指定的位置实施 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);
}
这是第二个 activity、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);
}
}
非常感谢所有输入,谢谢!
对于每个 case 语句,您只需通过 Intent
.
Activity
开始
例如,您希望在选择 navigation_item_1
时启动 Playboard
activity。
您可以将此代码添加到特定的 case
。
case R.id.navigation_item_1:
Intent i = new Intent(MainActivity.this, Playboard.class);
startActivity(i);
break;
一个警告:如果抽屉盒上有任何动画,直接从主线程启动 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();
而不是
NavigationUI.setupWithNavController(navigationView, navController);
这样做
navigationView.setNavigationItemSelectedListener(new
NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_SHORT).show();
return false;
}
}
);