我正在为包含抽屉的Engineering Design项目开发Android应用。我的一组成员选择使用android.support.design.widget.NavigationView API来执行此操作,这一直在给我们带来一些问题。我们的MainActivity类实现了onNavigationItemSelectedListener,据我了解,它充当抽屉和其他导航栏中项目的侦听器。问题是,当选择这些导航项之一时,不会调用已实现的onNavigationItemSelected方法(我知道这一点,因为如果调用它,它应该通过LogCat发出消息)。我为该API找到的所有参考文献都表明,到目前为止我已经做对了所有事情,但是我显然错过了一些事情,因此可以提供任何帮助。
这是MainActivity类中的相关代码:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpNavComponents();
setUpMapComponents();
PathfinderDataHandler handler = new PathfinderDataHandler();
}
private void setUpNavComponents()
{
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
}
private void setUpMapComponents()
{
mapFrag = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
mapFrag.getMapAsync(this);
}
@Override
public boolean onNavigationItemSelected(MenuItem item)
{
// Handle navigation view item clicks here.
int id = item.getItemId();
Log.d(null, "onNavigationItemSelected Called");
if (id == R.id.buildings)
{
// Handle the camera action
Log.d(null, "Buildings selected");
}
else if (id == R.id.blue_phones)
{
Log.d(null, "Blue Phones selected");
}
else if (id == R.id.bus_stops)
{
Log.d(null, "Bus Stops selected");
}
else if (id == R.id.pedways)
{
Log.d(null, "Pedways selected");
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
这是来自activity_main_drawer.xml的代码(其中包含抽屉的所有项目)
<?xml version="1.0" encoding="utf-8"?>
<menu 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"
tools:showIn="navigation_view">
<group
android:checkableBehavior="all"
android:enabled="true">
<item
android:id="@+id/buildings"
android:title="@string/building_show_select"
app:actionLayout="@layout/switch_item"
app:showAsAction="always" />
<item
android:id="@+id/blue_phones"
android:title="@string/blue_phone_select"
app:actionLayout="@layout/switch_item"
app:showAsAction="always" />
<item
android:id="@+id/bus_stops"
android:title="@string/bus_stop_select"
app:actionLayout="@layout/switch_item"
app:showAsAction="always" />
<item
android:id="@+id/pedways"
android:title="@string/pedway_select"
app:actionLayout="@layout/switch_item"
app:showAsAction="always" />
</group>
这是activity_main.xml的代码,该代码将activity_main_drawer添加到NavigationView。
<?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"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible" />
<android.support.design.widget.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/activity_main_drawer">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
要注意的另一件事是,我使用的是开关项,而不是常规的菜单项。
在任何人说什么之前,是的,我知道这个API已被淘汰,我应该在AndroidX中使用NavigationUI,但是我没有时间重构应用程序,所以我必须解决这个问题。