1

您好我有抽屜式導航欄在我的主要活動Java類的Android應用程序,這是代碼導航抽屜的主要活動內容

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

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

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 

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

     //add this line to display menu1 when the activity is loaded 
     displaySelectedScreen(R.id.nav_menu1); 
    } 

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

    private void displaySelectedScreen(int itemId) { 

     //creating fragment object 
     Fragment fragment = null; 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 

     //initializing the fragment object which is selected 
     switch (itemId) { 
      case R.id.nav_menu1: 
       fragment = new gallary(); 
       break; 
      case R.id.nav_menu2: 
       fragment = new about(); 
       break; 
      case R.id.nav_menu3: 
       fragment = new contact(); 
       break; 
     } 

     //replacing the fragment 
     if (fragment != null) { 
      FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
      ft.replace(R.id.content_frame, fragment); 
      ft.commit(); 
     } 

     drawer.closeDrawer(GravityCompat.START); 
    } 


    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 

     //calling the method displayselectedscreen and passing the id of selected menu 
     displaySelectedScreen(item.getItemId()); 
     //make this method blank 
     return true; 
    } 
} 

,這是主要的活動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:id="@+id/drawer_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     tools:openDrawer="start"> 

     <include 
      layout="@layout/app_bar_main" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 

     <android.support.design.widget.NavigationView 
      android:id="@+id/nav_view" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_gravity="start" 
      android:fitsSystemWindows="true" 
      app:headerLayout="@layout/nav_header_main" 
      app:menu="@menu/activity_main_drawer" /> 

    </android.support.v4.widget.DrawerLayout> 

所以我想添加一些內容,如圖像和文本到應用程序的主要活動xml頁面,但是當我在導航的主要內容中添加它時,它看起來不錯,一切都還好,但是當我從導航抽屜中點擊例如gallary片段時把我帶到了新的頁面,裏面放着我放進去的內容ary xml但問題是主要活動的內容也出現與我從導航抽屜中點擊的所有片段頁面 如何讓每個片段的內容都顯示爲沒有主要活動的內容 任何人都瞭解我? @ _ @

+1

請加布局文件 –

+0

我編輯的問題,並將其添加 – mh9

回答

1

我建議你不要使用除navigationView以外的任何元素和將顯示導航抓取項目的屏幕的片段,因爲如果點擊某個項目,您將被重定向到該頁面,但稍後如果你想回到主要活動頁面,它幾乎是不可能的。無論你重新開始活動還是嘗試一些複雜的方法來實現這一點。

相反,你應該使用片段。您可以調用默認片段,該片段在onResume()方法中打開應用程序時將被加載。這個片段也應該在導航項目可供以後references.You能做到這樣的:

fragment = new YourFragment(); 
    transaction = getSupportFragmentManager().beginTransaction(); 
    transaction.replace(R.id.contentfragment,fragment).commitNow(); 

但是如果你還是想這樣做,你可以設置在MainActivity的元素知名度水漲船高。我不知道這種方法是否可行。

這應該對導航項目設置單擊

[your_element].setVisibility(View.GONE); 
0

你片段相互重疊。 只要確保你從抽屜中選擇一個導航項目時添加新片段之前刪除以前的片段:

FragmentManager fm = getSupportFragmentManager(); 
fm.bginTransaction() 
    .remove(oldFragment) 
    .add(newFragment) 
    .commit();