简体   繁体   English

我有一个导航抽屉,想打开另一个孤立的片段

[英]I have a Navigation Drawer and want open another isolated fragment

I have a navigation drawer and when i click on one button i want to go to other fragment where i can't access to the navigation drawer and anything else.我有一个导航抽屉,当我点击一个按钮时,我想转到其他片段,在那里我无法访问导航抽屉和其他任何东西。 I just want have a black arrow in the top-left to go back and return to the navigation drawer with all the fragments.我只想在左上角有一个黑色箭头,以便返回并返回带有所有片段的导航抽屉。 I tried somethings but don't works like i want normally navigator continues accessible and see bot layouts one over the other.我尝试了一些东西,但不起作用,就像我希望导航器通常可以继续访问并查看机器人布局一样。

MAIN FRAGMENTS CONTROLLER主要片段控制器

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

        Toolbar toolbar = findViewById(R.id.toolbar);
        DrawerLayout drawerLayout=findViewById(R.id.drawer);
        NavigationView navigationView=findViewById(R.id.nav_view);

        setSupportActionBar(toolbar);

        appBarConfiguration= new AppBarConfiguration.Builder(R.id.nav_home,
                R.id.nav_user_data,R.id.nav_join_group,
                R.id.nav_user_groups,R.id.nav_search_documents)
                .setDrawerLayout(drawerLayout).build();

        navController = Navigation
                .findNavController(this,R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this,navController,appBarConfiguration);
        NavigationUI.setupWithNavController(navigationView,navController);



    }

    @Override
    public boolean onSupportNavigateUp() {
        return NavigationUI.navigateUp(navController,appBarConfiguration)
                || super.onSupportNavigateUp();
    }

I'm trying with a botton on my ActionBar, when i click it should go to my new fragment (with a generic app back but with one back button)我正在尝试在我的 ActionBar 上使用一个按钮,当我单击它时,它应该转到我的新片段(返回一个通用应用程序,但只有一个返回按钮)

You simply should specify your action in navigation.xml file like others.您只需像其他人一样在navigation.xml文件中指定您的操作。

It will change hamburger icon by itself because you synchronize your navigation controller with NavDrawer in this two lines of code:它会自行更改汉堡图标,因为您在这两行代码中将导航控制器与 NavDrawer 同步:

NavigationUI.setupActionBarWithNavController(this,navController,appBarConfiguration);
NavigationUI.setupWithNavController(navigationView,navController);

We need to add one fragment in our navigation.xml (the file where we define our fragments associated with the Fragment and the layout) for the Fragment where we want to go.我们需要在我们的 navigation.xml(我们在其中定义与 Fragment 和布局相关联的片段的文件)中为我们想要去的 Fragment 添加一个片段。 Add there an action with a unique id and where we want to go in the fragment where we are.在那里添加一个具有唯一 id 的操作以及我们想要在我们所在的片段中去的位置。
Sorry, i know it's a little bit confused.抱歉,我知道这有点混乱。

NAVIGATION导航
We go from there我们从那里去

<fragment
        android:id="@+id/nav_user_data"
        android:name="com.example.androidapplication_reto2.project.activities.navigationfragments.SeeAndModifyUserDataFragment"
        android:label="User Data"
        tools:layout="@layout/fragment_see_and_modify_user_data"
        >
        <action
            android:id="@+id/action_nav_user_data_to_nav_modify_data"
            app:destination="@+id/nav_modify_data" />
    </fragment>`

To there到那里

<fragment
        android:id="@+id/nav_modify_data"
        android:name="com.example.androidapplication_reto2.project.activities.navigationfragments.ModifyUserDataFragment"
        android:label="Modify User Data"
        tools:layout="@layout/fragment_modify_user_data"
        />

With the action that we defined in the origen fragment.使用我们在 origen 片段中定义的操作。
ORIGEN FRAGMENT原始片段

And in the Fragment when you click on one button just happen this.而在 Fragment 中,当您单击一个按钮时就会发生这种情况。

Navigation.findNavController(getView()).navigate(R.id.action_nav_user_data_to_nav_modify_data);

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

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