0

내 앱에서 사이드 바 탐색 기능을 만들었습니다. 이제 클릭 이벤트를 만들고 싶습니다.사이드 바 탐색에서 클릭 이벤트를 만드는 방법

나는 그것을 찾아서 문서를 얻었지만 같은 것을 이해할 수는 없다.

누구든지 도움이 될만한 자료를 제안 해주세요. YouTube 튜토리얼이 도움이 될 것입니다.

내 navigation_menu

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:id="@+id/nav_account" 
     android:icon="@mipmap/ic_person_black_24dp" 
     android:title="My Account"/> 

    <item android:id="@+id/nav_setting" 
     android:icon="@mipmap/ic_settings_black_24dp" 
     android:title="Settings"/> 

    <item android:id="@+id/nav_logout" 
     android:icon="@mipmap/ic_logout_black_24dp" 
     android:title="Logout"/> 
    </menu> 

내 activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.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" 
    tools:context="com.example.lenovo.jdstudio.MainActivity"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

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

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Main Layout" 
      android:textAlignment="center" 
      android:textSize="24dp" /> 
    </LinearLayout> 

    <android.support.design.widget.NavigationView 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     app:headerLayout="@layout/navigation_header" 
     app:menu="@menu/navigation_menu"> 

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

</android.support.v4.widget.DrawerLayout> 
+0

여기 – ADM

+0

코드 탐색 서랍 코드를 게시 https://stackoverflow.com/questions/21796209/how-to-create-a-custom-navigation-drawer-in -android – Khemraj

답변

0

간단히 NavigationView.OnNavigationItemSelectedListener 인터페이스를 구현 한 후 다음과 같은 방법 onNavigationItemSelected(MenuItem menuItem)를 오버라이드 (override) :

public class MainActivity extends AppCompatActivity implements 
    NavigationView.OnNavigationItemSelectedListener{ 

    NavigationView navigationView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     // declaring the NavigationView 
     navigationView = (NavigationView) findViewById(R.id.navigationView); 
     // assigning the listener to the NavigationView 
     navigationView.setNavigationItemSelectedListener(this); 
    } 

    @Override 
    public boolean onNavigationItemSelected(MenuItem menuItem) { 

     menuItem.setChecked(true); 

     switch (menuItem.getItemId()) { 
      case R.id.nav_account: 
       // do you click actions for the first selection 
       break; 
      case R.id.nav_setting: 
       // do you click actions for the second selection 
       break; 
      case R.id.nav_logout: 
       // do you click actions for the third selection 
       break; 
     } 

     return true; 
    } 

} 

그리고 대한

확인 돈 ' 잊지 말고 id 당신의 XML 레이아웃에서 NavigationView에 :

<android.support.design.widget.NavigationView 
    android:id="@+id/navigationView" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    app:headerLayout="@layout/navigation_header" 
    app:menu="@menu/navigation_menu"> 

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

조각이나 새 활동을 만들어야합니까? 어떤게 더 좋아? –

+0

NavigationDrawer 홀더에 관해서는 활동이어야한다고 말하면서 NavigationDrawer가 리다이렉트 할 페이지에 대해 이야기하는 경우에는 모든 페이지에서 동일한 NavigationDrawer를 갖도록 fragmentsw해야합니다 동일한 기능을 모든 활동에서 다시 만들지 않아도됩니다. –

+0

도움을 주셔서 감사합니다 –