-1

Ich mag würde, wenn ich die Navigationsleiste Menü zu öffnen, und klicken Sie auf ein bestimmtes Menü, um es anstelle eines Fragments eine Aktivität öffnen kannWie eine Aktivität aus einer Navigationsleiste klicken Sie im Menü zu öffnen, statt Fragment

Hier ist mein Code einige von ihnen auf Fragmente und diejenigen, verbunden sind, die leer sind, würde ich mag sie öffnen eine Aktivität

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

    if (id == R.id.nav_home) { 
     //Set the fragment initially 
     FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction(); 
     fragmentTransaction.replace(R.id.fragment_container, new TabFragment()).commit(); 

    } else if (id == R.id.nav_gallery) { 
     //Set the fragment initially 
     FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction(); 
     fragmentTransaction.replace(R.id.fragment_container, new TabFragment2()).commit(); 

    } else if (id == R.id.nav_nearby) { 
     //Set the fragment initially 

    } else if (id == R.id.nav_search) { 
     //Set the fragment initially 

     Search fragment = new Search(); 
     android.support.v4.app.FragmentTransaction fragmentTransaction = 
       getSupportFragmentManager().beginTransaction(); 
     fragmentTransaction.replace(R.id.fragment_container, fragment); 
     fragmentTransaction.commit(); 

    } else if (id == R.id.nav_explore) { 
     //Set the fragment initially 

    } else if (id == R.id.nav_getverified) { 
     //Set the fragment initially 

     GetVerified fragment = new GetVerified(); 
     android.support.v4.app.FragmentTransaction fragmentTransaction = 
       getSupportFragmentManager().beginTransaction(); 
     fragmentTransaction.replace(R.id.fragment_container, fragment); 
     fragmentTransaction.commit(); 

    } else if (id == R.id.nav_getfeatured) { 
     //Set the fragment initially 

     GetFeatured fragment = new GetFeatured(); 
     android.support.v4.app.FragmentTransaction fragmentTransaction = 
       getSupportFragmentManager().beginTransaction(); 
     fragmentTransaction.replace(R.id.fragment_container, fragment); 
     fragmentTransaction.commit(); 

    } else if (id == R.id.nav_register) { 
     //Set the fragment initially 

     Register fragment = new Register(); 
     android.support.v4.app.FragmentTransaction fragmentTransaction = 
       getSupportFragmentManager().beginTransaction(); 
     fragmentTransaction.replace(R.id.fragment_container, fragment); 
     fragmentTransaction.commit(); 

    } else if (id == R.id.nav_login) { 
     //Set the fragment initially 

     Login fragment = new Login(); 
     android.support.v4.app.FragmentTransaction fragmentTransaction = 
       getSupportFragmentManager().beginTransaction(); 
     fragmentTransaction.replace(R.id.fragment_container, fragment); 
     fragmentTransaction.commit(); 

    } else if (id == R.id.nav_report) { 
     //Set the fragment initially 

     Report fragment = new Report(); 
     android.support.v4.app.FragmentTransaction fragmentTransaction = 
       getSupportFragmentManager().beginTransaction(); 
     fragmentTransaction.replace(R.id.fragment_container, fragment); 
     fragmentTransaction.commit(); 

    } 

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

zum Beispiel auf dem Suchmenü möchte ich es auf eine Aktivität zu verbinden.

Antwort

0

Zuerst müssen Sie Ihren Code aufräumen. Sie wiederholen die Instantiierung von FragmentTransaction in jedem Link-Objekt.

Sie können die folgenden als Klasse Membervariablen in Ihrer Aktivitätsklasse hinzufügen, z

public class MainActivity extends AppCompatActivity { 

    private Fragment fragment; 
    private FragmentTransaction fragmentTransaction; 

Ich habe Aufräumen onNavigationItemSelected (MenuItem Artikel) Methode und auch hinzugefügt, wie Sie Aktivität Seite im Navigationspunkt hinzufügen. `

startActivity(new Intent(this, ActivityToStart.class) 

in jeder if-Anweisung in dem Sie eine anderen Aktivität starten wollen, da Ihre Navigationsleiste in Ihrer Aktivitätsklasse ist:

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

    if (id == R.id.nav_home) { 
     //Set the fragment initially 
     fragment = new TabFragment(); 

    } else if (id == R.id.nav_gallery) { 
     //Set the fragment initially 
     fragment = new TabFragment2(); 

    } else if (id == R.id.nav_nearby) { 
     //Set the fragment initially 
     Intent intent = new Intent(YourActivity.this, NearByActivity.class); 
     startActivity(intent); 
     finish(); 

    } else if (id == R.id.nav_search) { 
     //Set the fragment initially 
     fragment = new Search(); 

    } else if (id == R.id.nav_explore) { 
     //Set the fragment initially 
     Intent intent = new Intent(YourActivity.this, ExploreActivity.class); 
     startActivity(intent); 
     finish(); 

    } else if (id == R.id.nav_getverified) { 
     //Set the fragment initially 
     fragment = new GetVerified(); 

    } else if (id == R.id.nav_getfeatured) { 
     //Set the fragment initially 
     fragment = new GetFeatured(); 

    } else if (id == R.id.nav_register) { 
     //Set the fragment initially 
     fragment = new Register(); 

    } else if (id == R.id.nav_login) { 
     //Set the fragment initially 
     fragment = new Login(); 

    } else if (id == R.id.nav_report) { 
     //Set the fragment initially 
     fragment = new Report(); 
    } 

    fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
    fragmentTransaction.replace(R.id.fragment_container, fragment).commit(); 

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

Hallo, vielen Dank, aber ich bekomme einen Fehler "kann LogoutIntent nicht beheben" –

+0

Ich habe die Antwort aktualisiert – Inducesmile

+0

Vielen Dank sooo! es hat perfekt funktioniert –

0

Sie können ganz einfach ein Intent von starten.

Sie können auch den Fragment-Ersatz-Code in einer separaten Funktion aufschreiben, um etwas wiederverwendbaren Code zu erstellen, anstatt ihn für jeden Element-Klick zu schreiben.

Hoffe es hilft!

Verwandte Themen