英文:
How to open New Activity by clicking Navigation Drawer Item in Android
问题
如何通过点击导航抽屉菜单项来打开新的活动?
Share_Home.java
public class Share_Home extends AppCompatActivity {
private long backPressedTime;
private ActionBarDrawerToggle toggle;
private DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share_home);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
tabs.setTabMode(TabLayout.MODE_SCROLLABLE);
tabs.getTabAt(1).select();
toggle = new ActionBarDrawerToggle(this, drawer, R.string.open, R.string.close);
drawer.addDrawerListener(toggle);
toggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (toggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_share_home.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.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/nav_menu_item" />
<include
layout="@layout/activity_tabs_menu"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.drawerlayout.widget.DrawerLayout>
nav_menu_item.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/nav_Setting"
android:icon="@drawable/setting_icon"
android:title="@string/nav_Setting" />
<item
android:id="@+id/nav_Help"
android:icon="@drawable/help_icon"
android:title="@string/nav_Help" />
<item
android:id="@+id/nav_Ratings"
android:icon="@drawable/rating_icon"
android:title="@string/nav_Ratings" />
<item
android:id="@+id/nav_About"
android:icon="@drawable/about_icon"
android:title="@string/nav_About" />
</menu>
英文:
How can I open New Activity by clicking a Navigation Drawer Menu Item?
Share_Home.java
public class Share_Home extends AppCompatActivity {
private long backPressedTime;
private ActionBarDrawerToggle toggle;
private DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share_home);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
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.
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
tabs.setTabMode(TabLayout.MODE_SCROLLABLE);
tabs.getTabAt(1).select();
toggle = new ActionBarDrawerToggle(this, drawer, R.string.open, R.string.close);
drawer.addDrawerListener(toggle);
toggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (toggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_share_home.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.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/nav_menu_item" />
<include
layout="@layout/activity_tabs_menu"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.drawerlayout.widget.DrawerLayout>
nav_menu_item.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/nav_Setting"
android:icon="@drawable/setting_icon"
android:title="@string/nav_Setting" />
<item
android:id="@+id/nav_Help"
android:icon="@drawable/help_icon"
android:title="@string/nav_Help" />
<item
android:id="@+id/nav_Ratings"
android:icon="@drawable/rating_icon"
android:title="@string/nav_Ratings" />
<item
android:id="@+id/nav_About"
android:icon="@drawable/about_icon"
android:title="@string/nav_About" />
</menu>
答案1
得分: 3
给你的 NavigationView
设置一个 NavigationItemSelectedListener
:
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
int id = menuItem.getItemId();
if (id == R.id.xxx){
Intent newIntent = new Intent(this, NewActivity.class);
startActivity(newIntent);
}
return true;
}
});
同时修改你的布局。
从:
<androidx.drawerlayout.widget.DrawerLayout>
<include layout="@layout/app_bar_main" />
<com.google.android.material.navigation.NavigationView/>
<include layout="@layout/activity_tabs_menu"/>
</androidx.drawerlayout.widget.DrawerLayout>
改为:
<androidx.drawerlayout.widget.DrawerLayout>
<include layout="@layout/app_bar_main" />
<com.google.android.material.navigation.NavigationView/>
</androidx.drawerlayout.widget.DrawerLayout>
将 <include layout="@layout/activity_tabs_menu"/>
移动到 app_bar_main
布局中。
英文:
Set a NavigationItemSelectedListener
to you NavigationView
:
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
int id=menuItem.getItemId();
if (id == R.id.xxx){
Intent newIntent = new Intent(this, NewActivity.class);
startActivity(newIntent);
}
return true;
}
});
Also change your layout.
Instead of:
<androidx.drawerlayout.widget.DrawerLayout >
<include layout="@layout/app_bar_main" />
<com.google.android.material.navigation.NavigationView/>
<include layout="@layout/activity_tabs_menu"/>
</androidx.drawerlayout.widget.DrawerLayout>
Use:
<androidx.drawerlayout.widget.DrawerLayout >
<include layout="@layout/app_bar_main" />
<com.google.android.material.navigation.NavigationView/>
</androidx.drawerlayout.widget.DrawerLayout>
moving the <include layout="@layout/activity_tabs_menu"/>
inside the app_bar_main
layout.
答案2
得分: 0
在你的NavigationView中获取一个监听器,用于监听所选项目。因此,您可以设置监听器,然后在导航抽屉中按下特定项目时启动一个活动,就像这样:
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.menu_item_1:
Intent intent = new Intent(this, ItemActivity.class);
startActivity(intent);
break;
}
}
});
<details>
<summary>英文:</summary>
You get a listener in your NavigationView that listens to the item pressed. So you can set the listener and then start an activity when a particular item is pressed in the NavigationDrawer, like so:
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.menu_item_1:
Intent intent = new Intent(this, ItemActivity.class);
startActivity(intent);
break;
}
}
});
</details>
# 答案3
**得分**: 0
以下是翻译好的部分:
```java
@Override
public boolean onOptionsItemSelected(MenuItem item) {
final int itemId = item.getItemId();
if (itemId == R.id.nav_Setting) {
Intent newIntent = new Intent(getApplicationContext(), YourSettingActivity.class);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(newIntent);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.nav_menu_item, menu);
return super.onCreateOptionsMenu(menu);
}
英文:
Here is how you can do it:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
final int itemId = item.getItemId();
if (itemId == R.id.nav_Setting) {
Intent newIntent = new Intent(getApplicationContext(), YourSettingActivity.class);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(newIntent);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.nav_menu_item, menu);
return super.onCreateOptionsMenu(menu);
}
答案4
得分: 0
你必须重写 onNavigationItemSelected
方法并检查所选菜单项的 id,然后打开相应的 Activity:
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.activity1:
startActivity(new Intent(this, Activity1.class));
break;
case R.id.activity2:
startActivity(new Intent(this, Activity2.class));
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
同时,记得在菜单布局中的每个项目中添加 id
:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/activity1"
android:title="@string/activity1" />
<item
android:id="@+id/activity2"
android:title="@string/activity2" />
</menu>
英文:
You have to override onNavigationItemSelected
and check the id of the selected menu item and then open Activity:
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.activity1:
startActivity(new Intent(this, Activity1.class));
break;
case R.id.activity2:
startActivity(new Intent(this, Activity2.class));
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
And remember to add id
to every item in menu layout:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/activity1"
android:title="@string/activity1" />
<item
android:id="@+id/activity2"
android:title="@string/activity2" />
</menu>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论