抽屉菜单在 Click 事件上没有做任何事情
drawer menu not doing anything on Click event
我在我的应用程序中实现了导航抽屉,但它的菜单在点击时不起作用。我不知道出了什么问题,但是点击事件没有做任何事情,也没有烘烤 message.Click 抽屉菜单中的任何东西都没有做 anything.Firstly 其他按钮也没有工作,但是将它们添加到单独的布局中解决了 issue.but抽屉菜单仍然无法使用
这是我的 xml :
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/drawer_menu" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="@layout/app_bar_main" />
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
在 java 我正在使用它 :
public class AuthorMainScreen extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawerLayout;
NavigationView navigationView;
private ActionBarDrawerToggle actionBarDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_author_navigation);
viewDeclaration();
drawerLayout.addDrawerListener(actionBarDrawerToggle);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (actionBarDrawerToggle.onOptionsItemSelected(item))
return true;
return super.onOptionsItemSelected(item);
}
private void viewDeclaration() {
newSurveyBtn = findViewById(R.id.new_surveys_button);
surveyWithRef = findViewById(R.id.get_survey_button);
surveyResult = findViewById(R.id.analyze_survey);
drawerLayout = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.navigation_view);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.menu_share:
System.out.println("ss" + "coming");
Toast.makeText(getApplicationContext(), "Share", Toast.LENGTH_SHORT).show();
break;
case R.id.menu_survey_count:
Toast.makeText(getApplicationContext(), "Surveys", Toast.LENGTH_SHORT).show();
break;
case R.id.menu_logout:
Toast.makeText(getApplicationContext(), "logout", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
您没有初始化 actionBarDrawerToggle。你必须这样做。
此问题的解决方案在 XML 文件中,将导航视图置于 include 标记下方,它会起作用
改变这个
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/drawer_menu" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="@layout/app_bar_main" />
</LinearLayout>
对此
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="@layout/app_bar_main" />
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/drawer_menu" />
当你在使用AndroidX的时候尽量使用相关的方法。
当你想在那个特定的片段中实现片段时添加宿主片段activity:
xml:
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<fragment
android:id="@+id/host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/navigation_graph"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="@layout/app_bar_main" />
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/drawer_menu" />
现在使用导航图,您需要在其中实现您将要在 activity 中使用的片段:
navigation_graph.xml:
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/navigation_graph"
app:startDestination="@id/profile">
<fragment
android:id="@+id/profile"
android:name="com.example.jetpacknavigationexample.Fragments.ProfileFragment"
android:label="@string/profile"
tools:layout="@layout/fragment_profile" />
<fragment
android:id="@+id/features"
android:name="com.example.jetpacknavigationexample.Fragments.FeaturesFragment"
android:label="@string/features"
tools:layout="@layout/fragment_features" />
<fragment
android:id="@+id/signout"
android:name="com.example.jetpacknavigationexample.Fragments.ProfileFragment"
android:label="@string/signout"
tools:layout="@layout/fragment_signout" />
</navigation>
在您的 java class 添加实现导航组件代码:
public class AuthorMainScreen extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawerLayout;
NavigationView navigationView;
private ActionBarDrawerToggle actionBarDrawerToggle;
//Add this
NavController mNavController;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_author_navigation);
//add this
mNavController = Navigation.findNavController(this,R.id.host_fragment);
NavigationUI.setupActionBarWithNavController(this, mNavController, drawerLayout);
NavigationUI.setupWithNavController(navigationView,mNavController);
mNavigationView.setNavigationItemSelectedListener(this);
viewDeclaration();
drawerLayout.addDrawerListener(actionBarDrawerToggle);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (actionBarDrawerToggle.onOptionsItemSelected(item))
return true;
return super.onOptionsItemSelected(item);
}
private void viewDeclaration() {
newSurveyBtn = findViewById(R.id.new_surveys_button);
surveyWithRef = findViewById(R.id.get_survey_button);
surveyResult = findViewById(R.id.analyze_survey);
drawerLayout = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.navigation_view);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.menu_share:
System.out.println("ss" + "coming");
Toast.makeText(getApplicationContext(), "Share",
Toast.LENGTH_SHORT).show();
break;
case R.id.menu_survey_count:
Toast.makeText(getApplicationContext(), "Surveys",
Toast.LENGTH_SHORT).show();
break;
case R.id.menu_logout:
Toast.makeText(getApplicationContext(), "logout",
Toast.LENGTH_SHORT).show();
break;
}
return true;
}
您现在可以测试您的应用程序了。实施后告诉我。
我在我的应用程序中实现了导航抽屉,但它的菜单在点击时不起作用。我不知道出了什么问题,但是点击事件没有做任何事情,也没有烘烤 message.Click 抽屉菜单中的任何东西都没有做 anything.Firstly 其他按钮也没有工作,但是将它们添加到单独的布局中解决了 issue.but抽屉菜单仍然无法使用
这是我的 xml :
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/drawer_menu" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="@layout/app_bar_main" />
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
在 java 我正在使用它 :
public class AuthorMainScreen extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawerLayout;
NavigationView navigationView;
private ActionBarDrawerToggle actionBarDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_author_navigation);
viewDeclaration();
drawerLayout.addDrawerListener(actionBarDrawerToggle);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (actionBarDrawerToggle.onOptionsItemSelected(item))
return true;
return super.onOptionsItemSelected(item);
}
private void viewDeclaration() {
newSurveyBtn = findViewById(R.id.new_surveys_button);
surveyWithRef = findViewById(R.id.get_survey_button);
surveyResult = findViewById(R.id.analyze_survey);
drawerLayout = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.navigation_view);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.menu_share:
System.out.println("ss" + "coming");
Toast.makeText(getApplicationContext(), "Share", Toast.LENGTH_SHORT).show();
break;
case R.id.menu_survey_count:
Toast.makeText(getApplicationContext(), "Surveys", Toast.LENGTH_SHORT).show();
break;
case R.id.menu_logout:
Toast.makeText(getApplicationContext(), "logout", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
您没有初始化 actionBarDrawerToggle。你必须这样做。
此问题的解决方案在 XML 文件中,将导航视图置于 include 标记下方,它会起作用
改变这个
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/drawer_menu" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="@layout/app_bar_main" />
</LinearLayout>
对此
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="@layout/app_bar_main" />
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/drawer_menu" />
当你在使用AndroidX的时候尽量使用相关的方法。
当你想在那个特定的片段中实现片段时添加宿主片段activity:
xml:
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<fragment
android:id="@+id/host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/navigation_graph"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="@layout/app_bar_main" />
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/drawer_menu" />
现在使用导航图,您需要在其中实现您将要在 activity 中使用的片段:
navigation_graph.xml:
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/navigation_graph"
app:startDestination="@id/profile">
<fragment
android:id="@+id/profile"
android:name="com.example.jetpacknavigationexample.Fragments.ProfileFragment"
android:label="@string/profile"
tools:layout="@layout/fragment_profile" />
<fragment
android:id="@+id/features"
android:name="com.example.jetpacknavigationexample.Fragments.FeaturesFragment"
android:label="@string/features"
tools:layout="@layout/fragment_features" />
<fragment
android:id="@+id/signout"
android:name="com.example.jetpacknavigationexample.Fragments.ProfileFragment"
android:label="@string/signout"
tools:layout="@layout/fragment_signout" />
</navigation>
在您的 java class 添加实现导航组件代码:
public class AuthorMainScreen extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawerLayout;
NavigationView navigationView;
private ActionBarDrawerToggle actionBarDrawerToggle;
//Add this
NavController mNavController;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_author_navigation);
//add this
mNavController = Navigation.findNavController(this,R.id.host_fragment);
NavigationUI.setupActionBarWithNavController(this, mNavController, drawerLayout);
NavigationUI.setupWithNavController(navigationView,mNavController);
mNavigationView.setNavigationItemSelectedListener(this);
viewDeclaration();
drawerLayout.addDrawerListener(actionBarDrawerToggle);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (actionBarDrawerToggle.onOptionsItemSelected(item))
return true;
return super.onOptionsItemSelected(item);
}
private void viewDeclaration() {
newSurveyBtn = findViewById(R.id.new_surveys_button);
surveyWithRef = findViewById(R.id.get_survey_button);
surveyResult = findViewById(R.id.analyze_survey);
drawerLayout = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.navigation_view);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.menu_share:
System.out.println("ss" + "coming");
Toast.makeText(getApplicationContext(), "Share",
Toast.LENGTH_SHORT).show();
break;
case R.id.menu_survey_count:
Toast.makeText(getApplicationContext(), "Surveys",
Toast.LENGTH_SHORT).show();
break;
case R.id.menu_logout:
Toast.makeText(getApplicationContext(), "logout",
Toast.LENGTH_SHORT).show();
break;
}
return true;
}
您现在可以测试您的应用程序了。实施后告诉我。