带有ActionBarDrawerToggle的Android菜单

问题描述 投票:-1回答:2

我有一个简单的任务:

创建一个包含少量项目的菜单,该项目在切换操作栏按钮上打开。

我的activity_main是:

<android.support.v4.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mt.gmtelandroid.MainActivity"
android:id="@+id/drawer_layout">



<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</LinearLayout>

<android.support.design.widget.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    app:menu="@menu/navigation_menu"
    android:layout_gravity="start"
    app:headerLayout="@layout/navigation_header"
    >

</android.support.design.widget.NavigationView>

和导航菜单

<menu xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/meni1" >
<item android:id="@+id/navhome"
    android:title="Home"
    android:icon="@mipmap/ic_home_black_24dp"
    >
</item>
<item android:id="@+id/navtasks" android:title="Zadaci"
    android:icon="@mipmap/ic_drive_eta_black_24dp"
    ></item>
<item android:id="@+id/navlogout" android:title="Odjava"
    android:icon="@mipmap/ic_close_black_24dp">
</item>
<item android:id="@+id/myname" android:title="Moj Nalog"
    android:icon="@mipmap/ic_account_circle_black_24dp">
</item>

主要活动是

   public class MainActivity extends AppCompatActivity
{

    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mtogle;

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

        mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        mtogle = new ActionBarDrawerToggle(this, mDrawerLayout,R.string.open_menu, R.string.close_menu);

        mDrawerLayout.addDrawerListener(mtogle);
        mtogle.syncState();

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {


        if (mtogle.onOptionsItemSelected(item))
        {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

我不知道如何在onMenuItemClick上添加一个监听器,这样我就可以处理当用户点击菜单项时会发生什么。我已经尝试在onOptionsItemSelected方法中添加一些代码,但是当我调试它时,我发现这个方法只在切换时调用,而不是单击菜单项!

android android-actionbar actionbardrawertoggle
2个回答
1
投票

您必须在NavigationView.OnNavigationItemSelectedListener中实现Activity的回调,如下所示,

public class HomeActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener

处理NavigationView的点击事件,如下面的代码

@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        Intent i;

         if (id == R.id.navhome) {
            // perform your action here
         } else if (id == R.id.navtasks) {                    
            // perform your action here
         } else if (id == R.id.navlogout) {
            // perform your action here
         }else if (id == R.id.myname) {
            // perform your action here
         }
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
}

0
投票

尝试添加:

callback实现NavigationView.OnNavigationItemSelectedListener,它将覆盖onNavigationItemSelected(MenuItem item){...}。您可以通过item.getItemId()将您的点击代码放在这里。

我希望这可以帮助你。

© www.soinside.com 2019 - 2024. All rights reserved.