Android onNavigationItemSelected未调用

时间:2018-10-19 09:03:18

标签: java android navigation drawer

希望有人可以在这里提供帮助,当我在抽屉式布局中单击某个项目时,不会调用onNavigationItemSelected方法。代码:

public class SuccessActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

private DrawerLayout drawerLayout;
private NavigationView navigationView;


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

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    AwesomeBar bar = findViewById(R.id.bar);
    bar.addAction(R.drawable.search, "Search");

    navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    bar.setOnMenuClickedListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            drawerLayout.openDrawer(Gravity.START);
        }
    });
}

    @Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    System.out.println("HI");
    return false;
}

}

activity_success.xml如下所示:

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:anroid="http://schemas.android.com/apk/res-auto"
    tools:context=".SuccessActivity">

    <com.github.florent37.awesomebar.AwesomeBar
        android:id="@+id/bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:elevation="4dp" />

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/map"
        tools:context=".MapsActivity"
        android:name="com.google.android.gms.maps.SupportMapFragment" />


</RelativeLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:menu="@menu/drawer_menu" />

我希望在控制台中选择菜单项时看到“嗨”,但什么也没有发生。我看过其他线程,但无法真正弄清我错过的内容。

我们非常感谢您的帮助!

0 个答案:

没有答案