2016-01-04 2 views
0

Я использую новый NavigationView, потому что я обновил свой проект до API 23, хорошо работает, но как я могу установить фрагмент по умолчанию? фрагмент, который открывается для открытия приложения. Это мой код, спасибо за совет.Android NavigationView set default fragment

public class MainActivity extends AppCompatActivity { 

    private Toolbar toolbar; 
    private NavigationView navigationView; 
    private DrawerLayout drawerLayout; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // Initializing Toolbar and setting it as the actionbar 
     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     //Initializing NavigationView 
     navigationView = (NavigationView) findViewById(R.id.navigation_view); 

     //Setting Navigation View Item Selected Listener to handle the item click of the navigation menu 
     navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 

      // This method will trigger on item Click of navigation menu 
      @Override 
      public boolean onNavigationItemSelected(MenuItem menuItem) { 


       //Checking if the item is in checked state or not, if not make it in checked state 
       if(menuItem.isChecked()) menuItem.setChecked(false); 
       else menuItem.setChecked(true); 

       //Closing drawer on item click 
       drawerLayout.closeDrawers(); 

       //Check to see which item was being clicked and perform appropriate action 
       switch (menuItem.getItemId()){ 
    //Replacing the main content with ContentFragment Which is our Inbox View; 
        case R.id.inbox: 
         Toast.makeText(getApplicationContext(),"Inbox Selected",Toast.LENGTH_SHORT).show(); 
         ContentFragment fragment = new ContentFragment(); 
         android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
         fragmentTransaction.replace(R.id.frame,fragment); 
         fragmentTransaction.commit(); 
         return true; 

        case R.id.starred: 
         Toast.makeText(getApplicationContext(),"Stared Selected",Toast.LENGTH_SHORT).show(); 
         return true;      
        default: 
         Toast.makeText(getApplicationContext(),"Somethings Wrong",Toast.LENGTH_SHORT).show(); 
         return true; 
       } 
      } 
     }); 

     // Initializing Drawer Layout and ActionBarToggle 
     drawerLayout = (DrawerLayout) findViewById(R.id.drawer); 
     ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.openDrawer, R.string.closeDrawer){ 

      @Override 
      public void onDrawerClosed(View drawerView) { 
       // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank 
       super.onDrawerClosed(drawerView); 
      } 

      @Override 
      public void onDrawerOpened(View drawerView) { 
       // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank 

       super.onDrawerOpened(drawerView); 
      } 
     }; 

     //Setting the actionbarToggle to drawer layout 
     drawerLayout.setDrawerListener(actionBarDrawerToggle); 

     //calling sync state is necessay or else your hamburger icon wont show up 
     actionBarDrawerToggle.syncState(); 

    } 


} 

ответ

2

Вы можете добавить фрагмент в вашем layout.activity_main или

в activity_main макета добавить контейнер как FrameLayout и

private Fragment currentFragment; 

    @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.search_group_activity); 
      if (savedInstanceState == null){ 
       currentFragment = new DefaultFragment(); 
       getFragmentManager().beginTransaction() 
         .add(R.id.frame, currentFragment).commit(); 
      } 
    } 

и в слушателе

    case R.id.inbox: 
        Toast.makeText(getApplicationContext(),"Inbox Selected",Toast.LENGTH_SHORT).show(); 
        currentFragment = new ContentFragment(); 
        android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
        fragmentTransaction.replace(R.id.frame, currentFragment); 
        fragmentTransaction.commit(); 
        return true; 
6

Первого меню прибудет Элемент в onCreate метод:

 //Get menuItem index 0 
     if (savedInstanceState == null) { 
       MenuItem item = navigationView.getMenu().getItem(0); 
       onNavigationItemSelected(item); 
     } 

И в onNavigationItemSelected:

@Override 
public boolean onNavigationItemSelected(MenuItem item) { 
    // Handle navigation view item clicks here. 
    int id = item.getItemId(); 
    Fragment fragment = null; 
    switch (id) { 
     case R.id.nav_capture_credentials: 
      fragment = new TestFragment(); 
      break; 
     case R.id.nav_pending_credentials: 
      fragment = new TestOtehrFragment(); 
      break; 
     case R.id.nav_close_session: 
      //Logout 
      break; 
    } 

    if (fragment != null) { 
     FragmentManager fragmentManager = getSupportFragmentManager(); 
     fragmentManager.beginTransaction() 
       .replace(R.id.container, fragment).commit(); 
     //Set menuItem checked and title 
     item.setChecked(true); 
     setTitle(item.getTitle()); 
     //close navigation view 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
    } else { 
     Log.e(TAG, "Error to create a fragment"); 
    } 
    return false; 
}