Android

navigationview 클릭이벤트

Machine_웅 2018. 7. 8. 22:20
728x90
반응형

menu 디렉터리에

 

test2.xml을  만들어 준후   작성

 

 

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">

<group android:checkableBehavior="single">
<item
android:id="@+id/nav_camera"
android:icon="@drawable/ic_menu_camera"
android:title="Import" />
<item
android:id="@+id/nav_gallery"
android:icon="@drawable/ic_menu_gallery"
android:title="Gallery" />
<item
android:id="@+id/nav_slideshow"
android:icon="@drawable/ic_menu_slideshow"
android:title="Slideshow" />
<item
android:id="@+id/nav_manage"
android:icon="@drawable/ic_menu_manage"
android:title="Tools" />
</group>

<item android:title="Communicate">
<menu>
<item
android:id="@+id/nav_share"
android:icon="@drawable/ic_menu_share"
android:title="Share" />
<item
android:id="@+id/nav_send"
android:icon="@drawable/ic_menu_send"
android:title="Send" />
</menu>
</item>

</menu>

 

 

 

네비게이션 뷰에  적용

 

 



<!-- 드로어 레이아웃 안의 내용들 반드시 네비게이션뷰의 그래비티를 정해줘야한다.-->
<android.support.design.widget.NavigationView
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:id="@+id/navi_view"
app:headerLayout="@layout/herder"
app:menu="@menu/test2"
>




</android.support.design.widget.NavigationView>

 

메인 액티비티에 implements

public class MainActivity extends AppCompatActivity   implements NavigationView.OnNavigationItemSelectedListener 

 

 

네비게이션 뷰 설정

NavigationView mNavigationView = (NavigationView) findViewById(R.id.navi_view);

if (mNavigationView != null) {
mNavigationView.setNavigationItemSelectedListener(this);
}

 

 

작동할 내용 작성.


@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {


int id = item.getItemId();

if (id == R.id.nav_camera) {
Toast.makeText(MainActivity.this, "카메라", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_gallery) {
Toast.makeText(MainActivity.this, "갤러리", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_slideshow) {
Toast.makeText(MainActivity.this, "슬라이드쇼", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_manage) {
Toast.makeText(MainActivity.this, "툴", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_share) {
Toast.makeText(MainActivity.this, "공유", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_send) {
Toast.makeText(MainActivity.this, "보내기", Toast.LENGTH_SHORT).show();
}


DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer);
drawer.closeDrawer(GravityCompat.START);



return false;
}
728x90
반응형