简体   繁体   English

Android:更改导航抽屉应用程序中的当前内容,而不在抽屉菜单中显示项目

[英]Android: change current content in navigation drawer app, without showing item in drawer menu

I'm a beginner in Android development, and I'd like to create an app with a Navigation Drawer.我是 Android 开发的初学者,我想创建一个带有导航抽屉的应用程序。 I started with the default template from the Android Studio 'create project' window.我从 Android Studio 'create project' window 中的默认模板开始。

This is the code from the MainActivity, that sets up the drawer navigation.这是 MainActivity 中的代码,用于设置抽屉导航。

@Override
protected void onCreate(Bundle savedInstanceState) {
    
    super.onCreate(savedInstanceState);
    
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    mAppBarConfiguration = new AppBarConfiguration.Builder(
        R.id.nav_customers, R.id.nav_notes, R.id.nav_stats, R.id.nav_settings)
            .setDrawerLayout(drawer)
            .build();
    navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);
}

I've got a fragment that is the 'customers' page.我有一个片段,即“客户”页面。 This page contains a button the user can click on to create a new customer.此页面包含一个用户可以单击以创建新客户的按钮。 When that button is clicked, a new page should be opened that takes the input for creating the customer.单击该按钮时,应打开一个新页面,该页面接受用于创建客户的输入。 This new page should not be a navigation item the user can navigate to directly.这个新页面不应该是用户可以直接导航到的导航项。 The only way and reason to open that page should be by using the 'create customer' button in the customers view I mentioned.打开该页面的唯一方法和理由应该是使用我提到的客户视图中的“创建客户”按钮。

So, that new page should not be included in the menu of the navigation drawer.因此,该新页面不应包含在导航抽屉的菜单中。 At the same time, I do want to give this page its own Toolbar.同时,我确实想给这个页面一个自己的工具栏。

The two most important things to do, seem to be:最重要的两件事似乎是:

  • Add a navigation destination / page to the activity.向活动添加导航目的地/页面。 This page should not be included in the navigation drawer's menu.此页面不应包含在导航抽屉的菜单中。 Opening the page should be done programmatically.打开页面应该以编程方式完成。
  • The page must have its own Toolbar at the top of the screen.该页面必须在屏幕顶部有自己的工具栏。

It seems simple and I tried a lot, but I didn't find any solution so far.这似乎很简单,我尝试了很多,但到目前为止我没有找到任何解决方案。

You can check the doc :您可以查看文档

Define in your graph something like:在您的图表中定义如下:

<fragment android:id="@+id/a"
          android:name="com.example.myapplication.FragmentA"
          android:label="a"
          tools:layout="@layout/a">
    <action android:id="@+id/action_a_to_b"
            app:destination="@id/b"/>
</fragment>
<fragment android:id="@+id/b"
          android:name="com.example.myapplication.FragmentB"
          android:label="b"
          tools:layout="@layout/b">
</fragment>

and then just use to navigate:然后只需使用导航:

findNavController().navigate(R.id.action_a_to_b)

Check also this page :还要检查这个页面

Adding the top app bar to your activity works well when the app bar's layout is similar for each destination in your app.当应用程序栏的布局对于您应用程序中的每个目标都相似时,将顶部应用程序栏添加到您的 Activity 效果很好。 If, however, your top app bar changes substantially across destinations, then consider removing the top app bar from your activity and defining it in each destination fragment, instead.但是,如果您的顶部应用栏在目的地之间发生了显着变化,那么请考虑从您的活动中移除顶部应用栏并在每个目的地片段中定义它。

In the FragmentB in the on onCreateView you can inflate your layoutonCreateViewFragmentB中,您可以扩展布局

override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
): View? {
    val root = inflater.inflate(R.layout.fragment_B container, false)

In this layout you can put a Toolbar :在此布局中,您可以放置一个Toolbar

<LinearLayout>
    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        ... />
    ...
</LinearLayout>

and:和:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    val navController = findNavController()
    val appBarConfiguration = AppBarConfiguration(navController.graph)

    view.findViewById<Toolbar>(R.id.toolbar)
            .setupWithNavController(navController, appBarConfiguration)
}

The alternative is to setup the navigation in the Activity and use the OnDestinationChangedListener :另一种方法是在 Activity 中设置导航并使用OnDestinationChangedListener

navController.addOnDestinationChangedListener { _, destination, _ ->
   if(destination.id == R.id.b) {
       toolbar.visibility = View.GONE
       //....
   } else {
       toolbar.visibility = View.VISIBLE
       //.....
   }

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

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