[안드로이드] navigation view를 사용해서 안드로이드 어플을 제작중입니다. 상위메뉴를 눌렀을때, 하위메뉴가 나오도록 만들고싶은데 어떻게 해야할까요?


이미지

여기서 상위메뉴(맛집찾기)를 눌렀을때, 하위메뉴(지역별,종류별)가 나오도록 하고싶습니다. java소스에서 하면 된다는건 알것같은데 잘모르겠습니다. main.java//////////////////////////////////////////////////////////////////코드입니다. package com.example.seongwon.myapplication13;

import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.view.View; import android.support.design.widget.NavigationView; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;



}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();



    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }




    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.notice) {
        // Handle the camera action
    } else if (id == R.id.recommend) {

    } else if (id == R.id.serch) {

    } else if (id == R.id.ksw) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

} ///////////////////////////////////////////////////////////////////////////////////// 밑에는 메뉴xml입니다. /////////////////////////////////////////////////////////////////////////////////// <?xml version="1.0" encoding="utf-8"?>

<group android:checkableBehavior="single">
    <item
        android:id="@+id/notice"
        android:icon="@drawable/ic_menu_camera"
        android:title="공지사항" />
    <item
        android:id="@+id/recommend"
        android:icon="@drawable/ic_menu_gallery"
        android:title="맛집추천">
    <menu>

            <item android:id="@+id/kind"
                android:title="종류별"
                />
            <item android:id="@+id/area"
                android:title="지역별" />

    </menu>
    </item>

    <item
        android:id="@+id/serch"
        android:icon="@drawable/ic_menu_slideshow"
        android:title="위치찾기" />
    <item
        android:id="@+id/ksw"
        android:icon="@drawable/ic_menu_manage"
        android:title="KSW" />
</group>

<item android:title="Communicate">
    <menu>
        <item
            android:id="@+id/nav_share"
            android:icon="@drawable/ic_menu_share"
            android:title="맛집 게시판" />
        <item
            android:id="@+id/nav_send"
            android:icon="@drawable/ic_menu_send"
            android:title="요리 레시피 게시판" />
    </menu>
</item>

  • 2016년 11월 25일에 작성됨

조회수 37


로그인이 필요한 기능입니다.

Hashcode는 개발자들을 위한 무료 QnA사이트 입니다. 작성한 답변에 다른 개발자들이 댓글을 작성하거나 좋아요/싫어요를 할 수 있기 때문에 계정을 필요로 합니다.
► 로그인
► 계정만들기
Close