导航抽屉未处理点击事件。

huangapple go评论62阅读模式
英文:

Navigation Drawer is not handling click events

问题

问题1:我的导航抽屉不能处理点击事件。

问题2:它显示的是向后的箭头,而不是汉堡图标。

我已经尝试过搜索,但找不到答案。我还尝试了添加.bringToFront()方法,但仍然不起作用。

MainActivity.java:-

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    TextView navUserName = findViewById(R.id.textViewNav_username);
    TextView navUserEmail = findViewById(R.id.textViewNav_useremail);
    mAuth = FirebaseAuth.getInstance();
    currentUser = mAuth.getCurrentUser();
    DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference volunteerRef = dbRef.child("Volunteer").child(currentUser.getUid());
    if (currentUser == null) {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        finish();
    }
    
    drawer = findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle actionBarDrawerToggle= new ActionBarDrawerToggle(this, drawer, toolbar,
            R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(actionBarDrawerToggle);
    actionBarDrawerToggle.syncState();
    
    NavigationView navigationView = findViewById(R.id.nav_view);
    navigationView.bringToFront();
    navigationView.setNavigationItemSelectedListener(this);
    navigationView.setCheckedItem(R.id.nav_option_home);
    
    mAppBarConfiguration = new AppBarConfiguration.Builder(
            R.id.nav_option_profile, R.id.nav_option_contact, R.id.nav_option_about, R.id.nav_option_share, R.id.nav_option_logout)
            .setDrawerLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);
}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    Log.d("main1122", "inside listner");
    switch (item.getItemId()){
        case R.id.nav_option_home:
            Log.d("main1122", "Clicked item" + item.getItemId());
            getSupportFragmentManager().beginTransaction().replace(R.id.content_nav_drawer,
                    new HomeFragment()).commit();
            break;
        case R.id.nav_option_profile:
            Log.d("main1122", "Clicked item" + item.getItemId());
            getSupportFragmentManager().beginTransaction().replace(R.id.content_nav_drawer,
                    new profile()).commit();
            break;
        case R.id.nav_option_contact:
            getSupportFragmentManager().beginTransaction().replace(R.id.content_nav_drawer,
                    new contact()).commit();
            break;
        case R.id.nav_option_about:
            getSupportFragmentManager().beginTransaction().replace(R.id.content_nav_drawer,
                    new AboutFragment()).commit();
            break;
        case R.id.nav_option_share:
            Toast.makeText(this, "Share clicked", Toast.LENGTH_SHORT).show();
            break;
        case R.id.nav_option_logout:
            // TODO: Logout action
            break;
    }

    drawer.closeDrawer(GravityCompat.START);
    return true;
}

activity_main.xml(包含R.id.nav_view):-

<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_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_nav__drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.google.android.material.navigation.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_nav__drawer"
        app:menu="@menu/activity_nav__drawer_drawer" />

</androidx.drawerlayout.widget.DrawerLayout>

mobile_navigation.xml(包含我想在点击菜单项后启动的片段):-

<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/mobile_navigation"
    app:startDestination="@id/nav_home">

    <fragment
        android:id="@+id/nav_home"
        android:name="com.helpinghandsorg.helpinghands.ui.home.HomeFragment"
        android:label="@string/menu_home"
        tools:layout="@layout/fragment_home" />

    <!-- 其他片段 -->

</navigation>

activity_nav_drawer_drawer.xml(包含菜单项的ID):-

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navi_view">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_option_home"
            android:icon="@drawable/ic_home_black_24dp"
            android:title="@string/menu_home" />
        <item
            android:id="@+id/nav_option_profile"
            android:icon="@drawable/ic_account_circle_black_24dp"
            android:title="@string/menu_profile" />
        <item
            android:id="@+id/nav_option_contact"
            android:icon="@drawable/ic_email_black_24dp"
            android:title="@string/menu_contact" />
        <item
            android:id="@+id/nav_option_about"
            android:icon="@drawable/ic_info_black_24dp"
            android:title="@string/menu_about" />
        <item
            android:id="@+id/nav_option_share"
            android:icon="@drawable/ic_menu_share"
            android:title="@string/menu_share" />
        <item
            android:id="@+id/nav_option_logout"
            android:icon="@drawable/ic_exit_to_app_black_24dp"
            android:title="@string/menu_logout" />
    </group>
</menu>
英文:

Problem 1- My navigation drawer is not handling click events.

Problem 2- Instead of showing hamburger icon it is showing backward arrow.

I tried searching already but couldn't find answer. I also tried adding .bringToFront() method but still not working.

MainActivity.java:-

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TextView navUserName = findViewById(R.id.textViewNav_username);
TextView navUserEmail = findViewById(R.id.textViewNav_useremail);
mAuth = FirebaseAuth.getInstance();
currentUser = mAuth.getCurrentUser();
DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference volunteerRef = dbRef.child(&quot;Volunteer&quot;).child(currentUser.getUid());
//If current user is null go to login activity
if (currentUser == null) {
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
finish();
}
//Creates hamburger animated icon
drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle actionBarDrawerToggle= new ActionBarDrawerToggle(this, drawer, toolbar,
R.string.navigation_drawer_open,R.string.navigation_drawer_close);
drawer.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
//Sets click listner to navigation item 1/2
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.bringToFront();
navigationView.setNavigationItemSelectedListener(this);
navigationView.setCheckedItem(R.id.nav_option_home);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_option_profile, R.id.nav_option_contact, R.id.nav_option_about, R.id.nav_option_share,R.id.nav_option_logout)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
//Sets click listner to navigation item 2/2
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Log.d(&quot;main1122&quot;, &quot;inside listner&quot;);
switch (item.getItemId()){
case R.id.nav_option_home:
Log.d(&quot;main1122&quot;, &quot;Clicked item&quot; + item.getItemId());
getSupportFragmentManager().beginTransaction().replace(R.id.content_nav_drawer,
new HomeFragment()).commit();
break;
case R.id.nav_option_profile:
Log.d(&quot;main1122&quot;, &quot;Clicked item&quot; + item.getItemId());
getSupportFragmentManager().beginTransaction().replace(R.id.content_nav_drawer,
new profile()).commit();
break;
case R.id.nav_option_contact:
getSupportFragmentManager().beginTransaction().replace(R.id.content_nav_drawer,
new contact()).commit();
break;
case R.id.nav_option_about:
getSupportFragmentManager().beginTransaction().replace(R.id.content_nav_drawer,
new AboutFragment()).commit();
break;
case R.id.nav_option_share:
//TODO: Share app action
Toast.makeText(this,&quot;Share clicked&quot;,Toast.LENGTH_SHORT).show();
break;
case R.id.nav_option_logout:
//TODO: Logout action
break;
}
drawer.closeDrawer(GravityCompat.START);
//Return false will make menu item unselected(not highlighted)
return true;
}

activity_main.xml (Contains R.id.nav_view) :-

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.drawerlayout.widget.DrawerLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:id=&quot;@+id/drawer_layout&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:fitsSystemWindows=&quot;true&quot;
tools:openDrawer=&quot;start&quot;&gt;
&lt;include
layout=&quot;@layout/app_bar_nav__drawer&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot; /&gt;
&lt;com.google.android.material.navigation.NavigationView
android:id=&quot;@+id/nav_view&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;match_parent&quot;
android:layout_gravity=&quot;start&quot;
android:fitsSystemWindows=&quot;true&quot;
app:headerLayout=&quot;@layout/nav_header_nav__drawer&quot;
app:menu=&quot;@menu/activity_nav__drawer_drawer&quot; /&gt;
&lt;/androidx.drawerlayout.widget.DrawerLayout&gt;

mobile_navigation.xml (Contains fragments which I want to launch after clicking menu items) :-

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;navigation xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:id=&quot;@+id/mobile_navigation&quot;
app:startDestination=&quot;@+id/nav_home&quot;&gt;
&lt;fragment
android:id=&quot;@+id/nav_home&quot;
android:name=&quot;com.helpinghandsorg.helpinghands.ui.home.HomeFragment&quot;
android:label=&quot;@string/menu_home&quot;
tools:layout=&quot;@layout/fragment_home&quot;/&gt;
&lt;fragment
android:id=&quot;@+id/nav_about&quot;
android:name=&quot;com.helpinghandsorg.helpinghands.AboutFragment&quot;
android:label=&quot;@string/menu_about&quot;
tools:layout=&quot;@layout/fragment_about&quot;&gt;
&lt;action
android:id=&quot;@+id/action_nav_about_to_nav_home&quot;
app:destination=&quot;@id/nav_home&quot; /&gt;
&lt;/fragment&gt;
&lt;fragment
android:id=&quot;@+id/nav_contact&quot;
android:name=&quot;com.helpinghandsorg.helpinghands.ui.contact.contact&quot;
android:label=&quot;@string/menu_contact&quot;
tools:layout=&quot;@layout/contact_fragment&quot;&gt;
&lt;action
android:id=&quot;@+id/action_nav_contact_to_nav_home&quot;
app:destination=&quot;@id/nav_home&quot; /&gt;
&lt;/fragment&gt;
&lt;fragment
android:id=&quot;@+id/nav_profile&quot;
android:name=&quot;com.helpinghandsorg.helpinghands.ui.profile.profile&quot;
android:label=&quot;@string/menu_profile&quot;
tools:layout=&quot;@layout/profile_fragment&quot;&gt;
&lt;action
android:id=&quot;@+id/action_nav_profile_to_nav_home&quot;
app:destination=&quot;@id/nav_home&quot; /&gt;
&lt;/fragment&gt;
&lt;/navigation&gt;

activity_nav_drawer_drawer.xml (Contains Menu Item IDs):-

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;menu xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
tools:showIn=&quot;navi_view&quot;&gt;
&lt;group android:checkableBehavior=&quot;single&quot;&gt;
&lt;item
android:id=&quot;@+id/nav_option_home&quot;
android:icon=&quot;@drawable/ic_home_black_24dp&quot;
android:title=&quot;@string/menu_home&quot; /&gt;
&lt;item
android:id=&quot;@+id/nav_option_profile&quot;
android:icon=&quot;@drawable/ic_account_circle_black_24dp&quot;
android:title=&quot;@string/menu_profile&quot; /&gt;
&lt;item
android:id=&quot;@+id/nav_option_contact&quot;
android:icon=&quot;@drawable/ic_email_black_24dp&quot;
android:title=&quot;@string/menu_contact&quot; /&gt;
&lt;item
android:id=&quot;@+id/nav_option_about&quot;
android:icon=&quot;@drawable/ic_info_black_24dp&quot;
android:title=&quot;@string/menu_about&quot; /&gt;
&lt;item
android:id=&quot;@+id/nav_option_share&quot;
android:icon=&quot;@drawable/ic_menu_share&quot;
android:title=&quot;@string/menu_share&quot; /&gt;
&lt;item
android:id=&quot;@+id/nav_option_logout&quot;
android:icon=&quot;@drawable/ic_exit_to_app_black_24dp&quot;
android:title=&quot;@string/menu_logout&quot; /&gt;
&lt;/group&gt;
&lt;/menu&gt;

答案1

得分: 0

问题- 而不是显示汉堡图标,它显示为向后的箭头。

解决方案- 实际上,这是因为我没有在AppbarConfiguration.Builder中提供主页片段的ID。

之前-

mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_option_profile, R.id.nav_option_contact, R.id.nav_option_about, R.id.nav_option_share,R.id.nav_option_logout)
.setDrawerLayout(drawer)
.build();

之后-

mAppBarConfiguration = new AppBarConfiguration.Builder(R.id.nav_home,
R.id.nav_option_profile, R.id.nav_option_contact, R.id.nav_option_about, R.id.nav_option_share,R.id.nav_option_logout)
.setDrawerLayout(drawer)
.build();
英文:

Answer for my 2nd question.
PROBLEM- Instead of showing hamburger icon it is showing backward arrow.

Solution- Actually it was because I didn't provide ID of home fragment on my AppbarConfiguration.Builder

Before-

mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_option_profile, R.id.nav_option_contact, R.id.nav_option_about, R.id.nav_option_share,R.id.nav_option_logout)
.setDrawerLayout(drawer)
.build();

After-

mAppBarConfiguration = new AppBarConfiguration.Builder(R.id.nav_home,
R.id.nav_option_profile, R.id.nav_option_contact, R.id.nav_option_about, R.id.nav_option_share,R.id.nav_option_logout)
.setDrawerLayout(drawer)
.build();

huangapple
  • 本文由 发表于 2020年4月10日 15:37:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/61135891.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定