简体   繁体   English

导航抽屉主要活动内容

[英]navigation drawer main activity content

hi i have navigation drawer in my android application in the main activity java class and this is the code 嗨,我在主要活动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;
     }
 }

and this is the main activity xml code 这是主要的活动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>

so i want to add some content such an images and texts to main activity xml page of the app but when i add it in main content of navigation it looks good and everything okay but when i click on for example gallary fragment from the navigation drawer it took me to new page which has the content that i put in gallary xml but the problem is the content of main activity appear too with all fragment pages that i click from navigation drawer how can i make the content of each fragment appear without the content of main activity anyone understand me ? 所以我想在应用程序的主要活动xml页面中添加一些内容,例如图像和文本,但是当我将其添加到导航的主要内容中时,它看起来不错,并且一切正常,但是当我单击例如导航抽屉中的Gall碎片时,我将我带到新页面,该页面具有我放入gallery xml中的内容,但问题是主活动的内容也出现在我从导航抽屉中单击的所有片段页面上时,如何使每个片段的内容都显示而没有内容主要活动有人了解我吗? @_@ @ _ @

I would suggest you to not to use any element other than navigationView and the fragment which will show up your screen for the navigationDrawer items, because you if clicked on some item you will be redirected to that page but later if you ever wanted to get back to that mainactivity page it becomes nearly impossible.Either you restart the activity or try some complex method to achieve that. 我建议您不要使用navigationView和将在屏幕上显示navigationDrawer项目的片段以外的任何元素,因为如果单击某些项目,您将被重定向到该页面,但是以后如果您想返回进入该mainactivity页面几乎是不可能的。您可以重新启动该活动或尝试一些复杂的方法来实现该目的。

Instead you should use fragments. 相反,您应该使用片段。 You can call a default fragment which will be loaded on opening the app in onResume() method. 您可以调用默认片段,该片段将在onResume()方法中打开应用程序时加载。 This fragment should also be available in navigation item for later references.You can do it like this: 此片段也应该在导航项目中可用,以供以后参考。您可以这样操作:

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

But if you still wanted to do so, you can set the elements of the mainactivity to visibility to gone. 但是,如果您仍然想这样做,则可以将mainactivity的可见性元素设置为消失。 I don't know if this method will work. 我不知道这种方法是否行得通。

This should be set on navigation item click 这应该在导航项单击上设置

[your_element].setVisibility(View.GONE);

You fragments are overlapping each other. 您的片段彼此重叠。 Just make sure you remove the previous fragment before adding a new fragment when selecting a navigation item from the drawer: 从抽屉中选择一个导航项目时,只需确保删除前一个片段,然后再添加一个新片段:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM