-1

Ich habe eine Navigationsschublade Aktivität erstellt. Ich möchte ein Fragment erstellen, das geöffnet wird, wenn auf den entsprechenden Eintrag in der Liste geklickt wird.Wie implementiert man die Methode onNavigationItemSelected?

Wie kann ich dies implementieren, um ein Bild anzuzeigen, wenn Sie auf das entsprechende Element klicken?

+0

http://developer.android.com/training/implementing-navigation/nav-drawer.html – buczek

+0

Ich habe geschaffen navigation drawer activity direkt durch android studio und auf der entwickler-site haben sie unterschiedliche weise ..... –

Antwort

1

Lassen Sie mich einen einfachen und vollständigen Code teilen, um mit Navigation Drawer zu arbeiten. Die MainActivity-Klasse.

public class MainActivity extends AppCompatActivity implements 
    NavigationView.OnNavigationItemSelectedListener{ 

private NavigationView navigationView; 
private DrawerLayout drawerLayout; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

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

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer); 
    ActionBarDrawerToggle actionBarDrawerToggle = 
      new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open_drawer, R.string.close_drawer) { 
       @Override 
       public void onDrawerClosed(View drawerView){ 
        super.onDrawerClosed(drawerView); 
       } 

       @Override 
       public void onDrawerOpened(View drawerView){ 
        super.onDrawerOpened(drawerView); 
       } 
      }; 

    drawerLayout.setDrawerListener(actionBarDrawerToggle); 

    actionBarDrawerToggle.syncState(); 

} 

@Override 
public boolean onNavigationItemSelected(MenuItem item){ 

    int id = item.getItemId(); 

    switch (id){ 
     case R.id.item1: 
      /* getSupportFragmentManager().beginTransaction() 
        .add(R.id.container, new MySimpleFragment()) 
        .addToBackStack(null) 
        .commit();*/ 
      Intent intent = new Intent(this, Class1.class); 
      startActivity(intent); 
      break; 
     case R.id.item2: 
      intent = new Intent(this, Class2.class); 
      startActivity(intent); 
      break; 
     case R.id.item3: 
      intent = new Intent(this, Class3.class); 
      startActivity(intent); 
      break; 
     default: 
      break; 
    } 
    drawerLayout.closeDrawer(GravityCompat.START); 
    return true; 
} 

}

activity_main.xml Datei sieht wie folgt aus:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/drawer" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:context=".MainActivity"> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:weightSum="1" 
    android:padding="3dp" 
    tools:context=".MainActivity"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:elevation="5dp" 
     android:minHeight="?attr/actionBarSize" 
     android:background="@android:color/transparent"/> 

    <FrameLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/container" 
     android:layout_below="@id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     tools:context=".MainActivity" 
     tools:ignore="MergeRootFrame" 
     android:background="@drawable/jackson"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:textStyle="italic" 
      android:padding="30dp" 
      android:layout_marginLeft="50dp" 
      android:textSize="15dp" 
      android:layout_marginTop="40dp" 
      android:textColor="@color/red" 
      android:text="Just Beat It"/> 
    </FrameLayout> 

</RelativeLayout> 

<android.support.design.widget.NavigationView 
    android:id="@+id/navigation_view" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:background="@drawable/drawer" 
    app:itemIconTint="@color/colorAccent" 
    app:headerLayout="@layout/header" 
    app:menu="@menu/drawer"> 
</android.support.design.widget.NavigationView> 

Und ist das Layout für header.xml Datei

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="190dp" 
    android:background="@drawable/header" 
    android:orientation="vertical"> 

    <ImageView 
     android:layout_width="76dp" 
     android:layout_height="76dp" 
     android:id="@+id/profile_image" 
     android:layout_marginLeft="54dp" 
     android:layout_marginTop="50dp" 
     android:layout_centerVertical="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:src="@drawable/myphoto_cutmypic"/> 

    <TextView 
     android:id="@+id/txt1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/profile_image" 
     android:layout_marginLeft="54dp" 
     android:textStyle="bold" 
     android:paddingTop="10dp" 
     android:textColor="@color/red" 
     android:text="@string/my_name"/> 
    <TextView 
     android:id="@+id/txt2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/txt1" 
     android:textStyle="bold" 
     android:paddingTop="10dp" 
     android:layout_marginLeft="54dp" 
     android:textColor="@color/red" 
     android:text="@string/my_email"/> 

</RelativeLayout> 

Und, das Menü/Schublade.xml f ile wie folgt aussieht:

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
    <group android:checkableBehavior="single"> 
     <item 
      android:id="@+id/item1" 
      android:checked="false" 
      android:icon="@drawable/ic_call1" 
      android:title="item1"/> 

     <item 
      android:id="@+id/item2" 
      android:checked="false" 
      android:icon="@drawable/ic_call2" 
      android:title="item2"/> 

     <item 
      android:id="@+id/item3" 
      android:checked="false" 
      android:icon="@drawable/ic_call3" 
      android:title="item3"/> 

    </group> 
</menu> 

Wenn Sie irgendwelche Zweifel haben, antworten Sie auf diese Antwort