如何在 Android 中通过点击导航抽屉项目来打开新的活动。

huangapple go评论73阅读模式
英文:

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.drawerlayout.widget.DrawerLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:id=&quot;@+id/drawer_layout&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:fitsSystemWindows=&quot;true&quot;
tools:openDrawer=&quot;start&quot;&gt;
&lt;include
layout=&quot;@layout/app_bar_main&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot; /&gt;
&lt;com.google.android.material.navigation.NavigationView
android:id=&quot;@+id/nav_view&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;match_parent&quot;
android:layout_gravity=&quot;start&quot;
android:fitsSystemWindows=&quot;true&quot;
app:headerLayout=&quot;@layout/nav_header_main&quot;
app:menu=&quot;@menu/nav_menu_item&quot; /&gt;
&lt;include
layout=&quot;@layout/activity_tabs_menu&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot; /&gt;
&lt;/androidx.drawerlayout.widget.DrawerLayout&gt;

nav_menu_item.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;menu xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
&lt;item
android:id=&quot;@+id/nav_Setting&quot;
android:icon=&quot;@drawable/setting_icon&quot;
android:title=&quot;@string/nav_Setting&quot; /&gt;
&lt;item
android:id=&quot;@+id/nav_Help&quot;
android:icon=&quot;@drawable/help_icon&quot;
android:title=&quot;@string/nav_Help&quot; /&gt;
&lt;item
android:id=&quot;@+id/nav_Ratings&quot;
android:icon=&quot;@drawable/rating_icon&quot;
android:title=&quot;@string/nav_Ratings&quot; /&gt;
&lt;item
android:id=&quot;@+id/nav_About&quot;
android:icon=&quot;@drawable/about_icon&quot;
android:title=&quot;@string/nav_About&quot; /&gt;
&lt;/menu&gt;

答案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>

&lt;include layout="@layout/activity_tabs_menu"/&gt; 移动到 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:

&lt;androidx.drawerlayout.widget.DrawerLayout &gt;
&lt;include layout=&quot;@layout/app_bar_main&quot; /&gt;
&lt;com.google.android.material.navigation.NavigationView/&gt;
&lt;include layout=&quot;@layout/activity_tabs_menu&quot;/&gt;
&lt;/androidx.drawerlayout.widget.DrawerLayout&gt;

Use:

&lt;androidx.drawerlayout.widget.DrawerLayout &gt;
&lt;include layout=&quot;@layout/app_bar_main&quot; /&gt;
&lt;com.google.android.material.navigation.NavigationView/&gt;
&lt;/androidx.drawerlayout.widget.DrawerLayout&gt;

moving the &lt;include layout=&quot;@layout/activity_tabs_menu&quot;/&gt; 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:

&lt;menu xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
&lt;item
android:id=&quot;@+id/activity1&quot;
android:title=&quot;@string/activity1&quot; /&gt;
&lt;item
android:id=&quot;@+id/activity2&quot;
android:title=&quot;@string/activity2&quot; /&gt;
&lt;/menu&gt;

huangapple
  • 本文由 发表于 2020年9月11日 18:23:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63845221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定