2017-07-20 2 views
0

Je suis nouveau à ce Concept NavigationDrawer, je n'ai pas utilisé la barre d'outils dans MainActivity. J'utilise l'Appbar intégré pour cela j'ai ajouté l'option de navigation et ajouté les éléments à celui-ci. Maintenant, je veux savoir comment rendre cet article cliquable et en cliquant dessus, il devrait aller une autre activité.
MainActivity:Comment rendre les éléments de navigationDrawer comme cliquables dans la classe d'activité seulement et il devrait naviguer vers une autre activité

public class Main2Activity extends AppCompatActivity { 

    private DrawerLayout mDrawerLayout; 
    private ActionBarDrawerToggle mToggle; 

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

     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); 

     mToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.drawer_open,R.string.drawer_close); 

     mDrawerLayout.addDrawerListener(mToggle); 
     mToggle.syncState(); 


     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     if (mToggle.onOptionsItemSelected(item)){ 

      return true; 

     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

activity_main.xml:

<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:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.xyz.navigationdemo.Main2Activity" 
android:id="@+id/drawerLayout"> 


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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="2"> 

     <android.support.v4.view.ViewPager 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/viewPager"> 

     </android.support.v4.view.ViewPager> 

    </LinearLayout> 



    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:layout_marginTop="15dp"> 


     <ImageView 

      android:id="@+id/dua" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_marginRight="5dp" 
      android:layout_weight="1" 

      android:src="@drawable/bpone" 
      tools:ignore="ContentDescription,NestedWeights,RtlHardcoded" /> 

     <ImageView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:src="@drawable/bpone" 
      android:id="@+id/time" 
      android:clickable="true" 
      android:layout_marginRight="5dp" 
      tools:ignore="ContentDescription,RtlHardcoded" /> 

     <ImageView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:clickable="true" 
      android:id="@+id/gall" 
      android:src="@drawable/bpone" 
      tools:ignore="ContentDescription" /> 



    </LinearLayout> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:layout_marginTop="15dp" 
     android:layout_marginBottom="10dp"> 
     <ImageView 
      android:layout_width="0dp" 
      android:id="@+id/speech" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:clickable="true" 
      android:src="@drawable/bpone" 
      android:layout_marginRight="5dp" 
      tools:ignore="ContentDescription,NestedWeights,RtlHardcoded" /> 

     <ImageView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:id="@+id/batch" 
      android:clickable="true" 
      android:src="@drawable/bpone" 
      android:layout_marginRight="5dp" 
      tools:ignore="ContentDescription,RtlHardcoded" /> 

     <ImageView 
      android:layout_width="0dp" 
      android:id="@+id/about" 
      android:clickable="true" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:src="@drawable/bpone" 
      tools:ignore="ContentDescription" /> 

    </LinearLayout> 



</LinearLayout> 


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


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

navigation_menu.xml:

<item android:id="@+id/nav_account" 
     android:title="@string/alqalam" 
    android:icon="@drawable/home"/> 

<item android:id="@+id/nav_feedback" 
    android:title="@string/feedback" 
    android:icon="@drawable/feedback"/> 

<item android:id="@+id/nav_login" 
    android:title="@string/login" 
    android:icon="@drawable/login"/> 

<item android:id="@+id/nav_aboutus" 
    android:title="@string/aboutus" 
    android:icon="@drawable/aboutus"/> 

<item android:id="@+id/nav_share" 
    android:title="@string/share" 
    android:icon="@drawable/share"/> 

Répondre

1

Essayez cette

navigationView.setNavigationItemSelectedListener(new 
NavigationView.OnNavigationItemSelectedListener() { 

    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 

    int id = item.getItemId(); 

    if (id == R.id.nav_camera) { 
     // Handle the camera action 
     i=new Intent(youractivity.this,youractivity); 
     startActivity(i); 
    } else if (id == R.id.nav_gallery) { 
     i=new Intent(youractivity.this,youractivity); 
     startActivity(i); 
    } else if (id == R.id.nav_slideshow) { 
     //same 
    } else if (id == R.id.nav_manage) { 
     //same 
    } else if (id == R.id.nav_share) { 

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

    } 

return true; 
      } 

     }); 
+0

Merci, ça fonctionne très bien. –

+0

Vous êtes les bienvenus. ne me dérange pas d'upvote aussi bien –

+0

Merci, Ok je l'ai fait. –

0

la réponse à votre question est ci-dessous ..

public boolean onNavigationItemSelected(MenuItem item) { 
    // Handle navigation view item clicks here. 
    int id = item.getItemId(); 
    Intent i; 


    if (id == R.id.nav_camera) { 
     // Handle the camera action 
    i=new Intent(youractivity.this,youractivity); 
    startActivity(i); 
    } else if (id == R.id.nav_gallery) { 
    i=new Intent(youractivity.this,youractivity); 
    startActivity(i); 
    } else if (id == R.id.nav_slideshow) { 
    //same 
    } else if (id == R.id.nav_manage) { 
    //same 
    } else if (id == R.id.nav_share) { 

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

    } 

cela va démarrer votre activité sur le point de navigation .. cliquez

+0

Il ne fonctionne pas. –

+0

J'ai mis à jour la réponse, vous pouvez le vérifier maintenant .. –

+0

Je dois ajouter déclaration de retour ?? –