2015-07-13 177 views
0

我在Android Studio中使用導航抽屜。當我選擇在導航抽屜中的項目,我用下面的代碼:片段和活動

public void onNavigationDrawerItemSelected(int position) { 
    switch(position) 
     { 
      case 0: 
       Intent intent1 = new Intent(MainActivity.this,HomeActivity.class); 
       startActivity(intent1); 
       break; 
      case 1: 
       Intent intent2 = new Intent(MainActivity.this,DayActivity_1.class); 
       startActivity(intent2); 
       break; 
     } 
} 

當我打電話從抽屜式導航項目我的活動選擇的操作欄消失和全屏幕上打開活動。我如何管理導航抽屜不會消失?

回答

0

如果你想堅持導航抽屜,你應該改變內容片段,而不是顯示活動。

你的情況,這將是,改變

Intent intent1 = new Intent(MainActivity.this,HomeActivity.class); startActivity(intent1); 

有:

FragmentManager fragmentManager = ... 
FragmentTransaction transaction = fragmentManager.beginTransaction(); 
transaction.replace(R.id.your_fragment_container_id, new HomeFragment()) 
transaction.commit(); 
0

不使用DayActivity_1.class活動中使用,而不是片段,當你點擊導航項目JST創建片段和更換到抽屜式佈局

0

在您的主導航活動類中像下面一樣擴展FragmentActivity。

public class NavigationdrawerActivity extends FragmentActivity 

現在使用下面的代碼來解決問題

public void setContent(Fragment fragment) { 
     // Fragment fragment = new content_home(); 
     FragmentTransaction fragmentManager = getFragmentManager().beginTransaction(); 
     fragmentManager.setCustomAnimations(R.animator.enter_from_left, R.animator.exit_to_left); 
     // fragmentManager.beginTransaction() 
     fragmentManager.replace(R.id.mainContent, fragment).commit(); 

    } 

public void onNavigationDrawerItemSelected(int position) { 
    switch(position) 
     { 
      case 0: 

       break; 
      case 1: 
       Intent intent2 = new Intent(MainActivity.this,DayActivity_1.class); 
       startActivity(intent2); 
       break; 
     } 
} 

public void onNavigationDrawerItemSelected(int position) { 
    switch(position) 
     { 
      case 0: 
       Fragment homeActivityFragment = new HomeActivityFragment(); 
       //if you want to pass data to fragment 
        //Bundle bundle = new Bundle(); 
        //bundle.putString("id", "" + item.get("id")); 
        //homeActivityFragment.setArguments(bundle); 
       setContent(homeActivityFragment) 
       break; 
      case 1: 
       Fragment dayActivity_1Fragment = new DayActivity_1Fragment(); 
       setContent(dayActivity_1Fragment) 
       break; 
     } 
}