我正在尝试在我的应用程序上添加DrawerLayout,但是项目对点击没有响应。我已经尝试在控制台上记录输出,但是单击项目时什么也没有显示。这是我的代码
MainActivity
public class DashboardActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
Toolbar toolbar;
DrawerLayout drawerLayout;
NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_drawal);
drawerLayout = (DrawerLayout) findViewById(R.id.drawal_layout);
navigationView = (NavigationView) findViewById(R.id.navigation_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open_drawer,R.string.close_drawer);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
Log.i("info", String.valueOf(id));
switch (id){
case R.id.dashoard:
Toast.makeText(DashboardActivity.this, "This is dasbhard", Toast.LENGTH_SHORT).show();
case R.id.hire_mechanic_id:
Intent intents = new Intent(DashboardActivity.this, LIkelyProblemsActivity.class);
startActivity(intents);
break;
case R.id.tow_van_id:
Toast.makeText(getApplicationContext(),"Hire a Toyin Van", Toast.LENGTH_LONG).show();
break;
}
drawerLayout.closeDrawer(Gravity.START);
return true;
}
}
DrawerLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawal_layout"
android:fitsSystemWindows="false"
tools:openDrawer="start">
<include layout="@layout/activity_dashboard" />
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:id="@+id/navigation_view"
android:layout_gravity="start"
android:fitsSystemWindows="false"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
app:headerLayout="@layout/activity_navigation_header"
app:menu="@menu/navigation_menu"/>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
item.xml
<item android:title="Dashboard">
<menu>
<item
android:title="item 1"
android:icon="@drawable/mechanic"
android:id="@+id/dashoard"/>
<item
android:title="item 2"
android:icon="@drawable/mechanic"
android:id="@+id/hire_mechanic_id"/>
<item
android:title="item 3"
android:icon="@drawable/spareparts"
android:id="@+id/spare_parts_id"/>
<item
android:title="item 4"
android:id="@+id/tow_van_id"
android:icon="@drawable/toyin_van" />
</menu>
</item>
</menu>
我已经检查了抽屉布局,菜单和主要活动,但是我无法真正弄清楚问题出在哪里。任何可以帮助我的人。
答案 0 :(得分:0)
您忘记调用navigationView.setNavigationItemSelectedListener
侦听器,这就是为什么不调用导航项的原因,只需添加此行
navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(this)
希望它将对您有帮助!