実現したいこと
同じフラグメント内のnavigationdrawerだけを非表示にし、それ以外のレイアウトは表示させる設定にしたいです。
前提
navigationdrawerをactivityMain.xmlに記述し、すべての画面でnavigationdrawerが表示されるようにしました。しかし、特定のフラグメントではnavigationdrawerが表示されることが不都合になる場合もあるので、そういったフラグメントではnavigationdrawerを非表示にしたいです。
ところが、navigationdrawerを全体のコンテナとしているため、navigationdrawerを設定しているidを指定してView.GONEをつかって非表示にすると、コンテナ内のすべてのレイアウトが非表示になってしまい、何もない状態になってしまいます。navigationdrawerを実装するさいは、全体のコンテナとして設定することが推奨されていると聞いたので、レイアウトの設定は該当のソースコードのままにしていますが、そうするとどのようにして中のレイアウトだけを表示させればいいのでしょうか
詳しい方よろしくお願いいたします。
該当のソースコード
activityMain.xml
1<?xml version="1.0" encoding="utf-8"?> 2<androidx.drawerlayout.widget.DrawerLayout 3 android:id="@+id/drawer_layout" 4 xmlns:android="http://schemas.android.com/apk/res/android" 5 xmlns:tools="http://schemas.android.com/tools" 6 xmlns:app="http://schemas.android.com/apk/res-auto" 7 android:layout_width="match_parent" 8 android:layout_height="match_parent" 9 tools:context=".MainActivity"> 10 11 <LinearLayout 12 android:layout_width="match_parent" 13 android:layout_height="match_parent" 14 android:orientation="horizontal"> 15 16 <androidx.appcompat.widget.Toolbar 17 android:id="@+id/toolbar" 18 android:layout_width="match_parent" 19 android:layout_height="?attr/actionBarSize" 20 android:background="@color/secondary" 21 android:elevation="10dp" /> 22 23 </LinearLayout> 24 25 <androidx.constraintlayout.widget.ConstraintLayout 26 android:id="@+id/constraint_layout" 27 android:layout_width="match_parent" 28 android:layout_height="match_parent" > 29 30 <androidx.fragment.app.FragmentContainerView 31 android:id="@+id/nav_host_fragment" 32 android:name="androidx.navigation.fragment.NavHostFragment" 33 app:defaultNavHost="true" 34 android:layout_width="match_parent" 35 android:layout_height="match_parent" 36 app:layout_constraintStart_toStartOf="parent" 37 app:layout_constraintEnd_toEndOf="parent" 38 app:layout_constraintTop_toTopOf="parent" 39 app:layout_constraintBottom_toBottomOf="parent" 40 app:navGraph="@navigation/nav_graph" /> 41 42 <com.google.android.material.bottomnavigation.BottomNavigationView 43 android:id="@+id/bottom_navigation_view" 44 android:layout_width="match_parent" 45 android:layout_height="wrap_content" 46 app:layout_constraintStart_toStartOf="parent" 47 app:layout_constraintBottom_toBottomOf="parent" 48 app:menu="@menu/bottom_navigation_menu"/> 49 50 </androidx.constraintlayout.widget.ConstraintLayout> 51 52 <!--NavigationDrawer--> 53 <com.google.android.material.navigation.NavigationView 54 android:id="@+id/navigation_drawer" 55 android:layout_width="wrap_content" 56 android:layout_height="match_parent" 57 android:layout_gravity="start" 58 app:menu="@menu/navigation_drawer" /> 59 60</androidx.drawerlayout.widget.DrawerLayout>
試したこと
表示させたくないフラグメントで、下記のコードのように、先にdrawerlayout全体を非表示にしてから、その後にコンテナ内のレイアウト(constraintLayout)を表示するといったコードを追加してみましたが、何も表示されませんでした。エラーにはなっていません。この記述をしているのはフラグメントのonViewCreatedの部分です
kotlin
1activity?.findViewById<View>(R.id.drawer_layout)?.visibility = View.GONE 2activity?.findViewById<ConstraintLayout>(R.id.constraint_layout)?.visibility = View.VISIBLE
回答1件
あなたの回答
tips
プレビュー