带有ActionBarDrawerToggle

时间:2017-09-11 08:25:51

标签: android android-actionbar actionbardrawertoggle

我有一个简单的任务:

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

我的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方法中添加一些代码,但是当我调试它时,我发现这个方法仅在切换时调用,而不是单击菜单项!?

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

尝试添加:

回调实现NavigationView.OnNavigationItemSelectedListener , 它将覆盖onNavigationItemSelected(MenuItem item){...}。您可以通过 item.getItemId()

将您的点击代码放在此处

我希望这会对你有所帮助。