본문 바로가기
Android

안드로이드 스튜디오, JAVA]Material Design Navigation View에서, onNavigationItemSelected가 인식되지 않을 때

by 김마리님 2020. 7. 22.

https://stackoverflow.com/questions/48486796/setnavigationitemselectedlistener-not-working

 

setNavigationItemSelectedListener Not Working

My NavigationView onClick event is not working. Here are the code snippets I tried one by one, but nothing worked: Implementing NavigationView.OnNavigationItemSelectedListener using OnClick() Met...

stackoverflow.com

https://material.io/develop/android/components/navigation-view

 

Material Design

Build beautiful, usable products faster. Material Design is an adaptable system—backed by open-source code—that helps teams build high quality digital experiences.

material.io

 

 

내비게이션 클릭 -> 메뉴 클릭 이벤트 인식 시, 사용한 리스너가 onNavigationItemSelected 였는데, 이게 영 먹통이었다.

 

- MainActivity.java

package com.mary.navigationintentex01;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;

import com.google.android.material.navigation.NavigationView;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "Main_Activity";

    private NavigationView nav;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        nav = findViewById(R.id.nav);

        nav.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int id=item.getItemId();
                Log.d(TAG, "onNavigationItemSelected: id"+id);
                    if(id==R.id.menu1){
                        Intent intent=new Intent(MainActivity.this,SubActivity.class);
                        startActivity(intent);
                    }

                return true;
            }
        });
    }
}

 

- activity_main.xml

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:menu="@menu/nav_menu"
        android:layout_gravity="start|left"/>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorAccent">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textSize="60sp"
            android:text="메인 액티비티"/>

    </LinearLayout>




</androidx.drawerlayout.widget.DrawerLayout>

 

- activity_sub.xml

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SubActivity">

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:menu="@menu/nav_menu"
        android:layout_gravity="start|left"/>



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFACE">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textSize="60sp"
            android:text="서브 액티비티"/>

    </LinearLayout>


</androidx.drawerlayout.widget.DrawerLayout>

 

근데..

 

인식이 안 된다..;

당연히 디버그 코드도 안떠서 클릭 리스너가 안되는것 같아서 찾기 시작했는데, 의외의 황당한 이유였다.

xml 코드의 문제였는데, LinearLayout과 NavigationView 순서를 바꾸면 됐다........

 

- activity_main.xml

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorAccent">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textSize="60sp"
            android:text="메인 액티비티"/>

    </LinearLayout>


    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:menu="@menu/nav_menu"
        android:layout_gravity="start|left"/>

</androidx.drawerlayout.widget.DrawerLayout>

 

이렇게 LinearLayout 아래에 네비게이션을 두면...

이렇게 멀쩡하게 인식이 된다.

 

아마 네비게이션이 아래에 있다고 가정해서, 우리가 네비게이션을 눌러도 리니어 레이아웃이 클릭되었다고 인식해서 네비게이션 클릭 리스너가 먹기 전에 바깥 화면이 클릭되었다고 인식되서 햄버거가 닫히는 일이 발생한것 같다.

반응형