侧面导航抽屉项目单击不起作用
Posted
技术标签:
【中文标题】侧面导航抽屉项目单击不起作用【英文标题】:Side Navigation Drawer Item click not working 【发布时间】:2018-12-12 17:58:12 【问题描述】:请先阅读 在任何人将其标记为重复问题之前,因为它不是。
我使用了导航抽屉、底部导航栏和自定义操作栏。
底部导航栏和操作栏工作正常。 导航抽屉还显示其下方存在的菜单,但项目不可点击。
我已经尝试了所有与
相关的答案导航抽屉项目点击不起作用
但是我的问题还没有解决。 调试部分没有错误。
仪表板活动
package com.vicky.sampleApp;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v7.widget.Toolbar;
public class Dashboard extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener
//android.app.ActionBar actionbar;
TextView textview;
TextView textviewTitle;
private ActionBar toolbar;
private DrawerLayout drawer;
private ActionBarDrawerToggle toggle;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
Toolbar toolbar1 = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar1);
setActionBarText("HomePage");
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
toggle = new ActionBarDrawerToggle(
this, drawer, toolbar1, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.bringToFront();
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
toolbar = getSupportActionBar();
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.bottomNavigationView);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
private void setActionBarText(String text)
toolbar = getSupportActionBar();
toolbar.setBackgroundDrawable(getResources().getDrawable(R.drawable.default_appbar_theme));//line under the action bar
View viewActionBar = getLayoutInflater().inflate(R.layout.actionbar_title_text_layout, null);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(//Center the textview in the ActionBar !
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.MATCH_PARENT,
Gravity.CENTER);
textviewTitle = (TextView) viewActionBar.findViewById(R.id.actionbar_textview);
textviewTitle.setText(text);
toolbar.setCustomView(viewActionBar, params);
toolbar.setDisplayShowCustomEnabled(true);
toolbar.setDisplayShowTitleEnabled(false);
toolbar.setHomeButtonEnabled(true);
/*
private NavigationView.OnNavigationItemSelectedListener mSideNavigationItemSelectedListener
= new NavigationView.OnNavigationItemSelectedListener()
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item)
switch(item.getItemId())
case R.id.nav_camera:
Toast.makeText(Dashboard.this, "Camera", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Dashboard.this,AboutUs.class);
startActivity(intent);
break;
case R.id.nav_gallery:
Toast.makeText(Dashboard.this, "Gallery", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_slideshow:
Toast.makeText(Dashboard.this, "Slideshow", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_manage:
Toast.makeText(Dashboard.this, "Slideshow", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_share:
Toast.makeText(Dashboard.this, "Nav_share", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_send:
Toast.makeText(Dashboard.this, "nav_send", Toast.LENGTH_SHORT).show();
break;
drawer.closeDrawer(GravityCompat.START);
return true;
;
*/
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener()
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item)
Fragment fragment;
switch (item.getItemId())
case R.id.navigation_home:
setActionBarText("Home");
return true;
case R.id.navigation_appointments:
setActionBarText("Appointments");
return true;
case R.id.navigation_category:
setActionBarText("Category");
return true;
case R.id.navigation_profile:
setActionBarText("Profile");
return true;
return false;
;
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item)
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera)
Toast.makeText(this, "Camera", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this,AboutUs.class);
startActivity(intent);
else if (id == R.id.nav_gallery)
Toast.makeText(this, "Gallery", Toast.LENGTH_SHORT).show();
else if (id == R.id.nav_slideshow)
Toast.makeText(this, "Slideshow", Toast.LENGTH_SHORT).show();
else if (id == R.id.nav_manage)
Toast.makeText(this, "Nav_manage", Toast.LENGTH_SHORT).show();
else if (id == R.id.nav_share)
Toast.makeText(this, "Nav_share", Toast.LENGTH_SHORT).show();
else if (id == R.id.nav_send)
Toast.makeText(this, "nav_send", Toast.LENGTH_SHORT).show();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
dashboard.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_
android:layout_
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_
android:layout_
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
<FrameLayout
android:layout_
android:layout_>
<android.support.design.widget.AppBarLayout
android:layout_
android:layout_
android:theme="@style/CustomTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_
android:layout_
android:background="?attr/colorPrimary"
app:popupTheme="@style/CustomTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_
android:layout_
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:labelVisibilityMode="labeled"
app:itemBackground="@color/colorPrimary"
android:layout_gravity="bottom"
app:itemIconTint="@drawable/selector_bottom_nav_bar"
app:itemTextColor="@drawable/selector_bottom_nav_bar"
app:menu="@menu/navigation"/>
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
【问题讨论】:
BEFORE ANYONE MARK IT AS DUPLICATE PLEASE READ THE QUESTION.
好的先生
先生,您找到这不起作用的原因了吗?我已将图片添加到问题中以供参考,请查看它们。
【参考方案1】:
只需将您的NavigationView
放在FrameLayout
之后。检查以下代码:
<?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_
android:layout_
android:fitsSystemWindows="true"
tools:openDrawer="start">
<FrameLayout
android:layout_
android:layout_>
<android.support.design.widget.AppBarLayout
android:layout_
android:layout_
android:theme="@style/CustomTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_
android:layout_
android:background="?attr/colorPrimary"
app:popupTheme="@style/CustomTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_
android:layout_
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:labelVisibilityMode="labeled"
app:itemBackground="@color/colorPrimary"
android:layout_gravity="bottom"
app:itemIconTint="@drawable/selector_bottom_nav_bar"
app:itemTextColor="@drawable/selector_bottom_nav_bar"
app:menu="@menu/navigation"/>
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_
android:layout_
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
【讨论】:
非常感谢先生,这真是一个愚蠢的错误,不久前我只转移了顶部的那个部分。 SIR 请问我知道这个问题发生的原因... 因为 FrameLayout 与 NavigationView 重叠。 @VickySalunkhe 你知道吗。他们在 AndroidStudio 的模板抽屉导航项目中犯了同样的错误!感谢上帝,我找到了这个答案:) 这个答案真是救命稻草。花了这么多时间才发现我的 NavigationView 必须位于 xml 布局的底部。你做这个吗?!!!【参考方案2】:我正在搜索不同的关键字,但每次都登陆此页面。我遇到了无法单击导航菜单项的问题。
我认为对于那些在创建带有导航抽屉的项目时遇到这种奇怪行为的人来说很容易找到解决方案。
我花了好几个小时,但最后我必须以自己的方式找到它,而这种方式太简单了。 发帖希望对某人有所帮助。我的布局如下,项目点击不起作用。
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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_
android:layout_
android:fitsSystemWindows="true"
tools:openDrawer="start">
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_
android:layout_
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
<include
layout="@layout/app_bar_main"
android:layout_
android:layout_ />
</androidx.drawerlayout.widget.DrawerLayout>
我改成这个了,把导航视图放在底部就行了。
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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_
android:layout_
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_
android:layout_ />
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_
android:layout_
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
【讨论】:
您为我节省了数小时的搜索时间,但遗憾的是 Android Studio 在您创建新抽屉活动时默认创建第一个版本...【参考方案3】:我也有同样的问题。只需要将 NavigationView 放在 XML 代码的最后部分即可。
【讨论】:
【参考方案4】:您应该调用或包含框架布局 在next之前,导航参考代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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_
android:layout_
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_
android:layout_ />
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_
android:layout_
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
【讨论】:
以上是关于侧面导航抽屉项目单击不起作用的主要内容,如果未能解决你的问题,请参考以下文章