kotlin
1 2 override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 3 super.onViewCreated(view, savedInstanceState) 4 5 //コンポーネントの取得 6 val tagSelectSpinner = view.findViewById<Spinner>(R.id.tagSelect) 7 val button= view.findViewById<Button>(R.id.buttn) 8 9 //Fragmentの取得 10 val fragment = Fragment() 11 12 //Spinnerにセット(Tag) 13 val tagList = arrayListOf("a","b","c") 14 val adapter = context?.let { ArrayAdapter(it,android.R.layout.simple_spinner_item,tagList) } 15 tagSelectSpinner.adapter = adapter 16 17 18 //Spinnerを作成し値を設定 19 val spin = view.findViewById<Spinner>(R.id.aLine) 20 val ar = arrayListOf("aaa") 21 val adap = context?.let { ArrayAdapter(it,android.R.layout.simple_spinner_item,ar) } 22 spin.adapter = adap 23 24 25 26 //ボタンをクリック 27 button.setOnClickListener { 28 replaceFragment(fragment) 29 } 30 } 31 32 33 private fun replaceFragment(fragment:Fragment){ 34 val fragmanager = fragmentManager?.beginTransaction() 35 fragmanager?.replace(R.id.contentsArea,fragment)?.commit() 36 } 37 38} 39
xml
1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".FragmentSearch"> 8 9 <!-- Title --> 10 <TextView 11 android:id="@+id/title" 12 android:layout_width="match_parent" 13 android:layout_height="wrap_content" 14 app:layout_constraintEnd_toEndOf="parent" 15 app:layout_constraintStart_toStartOf="parent" 16 app:layout_constraintTop_toTopOf="parent" 17 android:background="@color/teal_200" 18 android:paddingLeft="20dp" 19 android:paddingTop="5dp" 20 android:paddingBottom="5dp" 21 android:text="@string/app_name" 22 android:textSize="40dp" /> 23 24 25 <!-- 絞り込み検索 --> 26 <!-- Tagの選択 --> 27 <LinearLayout 28 android:id="@+id/tagSelectArea" 29 android:layout_width="match_parent" 30 android:layout_height="wrap_content" 31 android:layout_marginTop="15dp" 32 app:layout_constraintTop_toBottomOf="@+id/title" 33 app:layout_constraintLeft_toLeftOf="parent" 34 app:layout_constraintRight_toRightOf="parent" 35 android:gravity="center" 36 android:orientation="horizontal"> 37 38 <TextView 39 android:layout_width="wrap_content" 40 android:layout_height="wrap_content" 41 android:layout_marginLeft="25dp" 42 android:layout_weight="1" 43 android:gravity="center" 44 android:text="@string/tagText"/> 45 46 <!-- SelectButton --> 47 <Spinner 48 android:id="@+id/tagSelect" 49 android:layout_width="wrap_content" 50 android:layout_height="wrap_content" 51 android:layout_weight="1" 52 android:background="@drawable/tag_border" 53 android:textAlignment="center" /> 54 55 <Button 56 android:id="@+id/button" 57 android:layout_width="wrap_content" 58 android:layout_height="wrap_content" 59 android:layout_marginRight="25dp" 60 android:layout_marginLeft="25dp" 61 android:backgroundTint="@color/tagItemColor" 62 android:text="@string/selectButton" 63 android:layout_weight="1" /> 64 </LinearLayout> 65 66 67 68 <!-- ここを別のフラグメントに切り替えたい --> 69 <FrameLayout 70 android:id="@+id/contentsArea" 71 android:layout_width="match_parent" 72 android:layout_height="wrap_content" 73 app:layout_constraintTop_toBottomOf="@+id/tagSelectArea" 74 app:layout_constraintLeft_toLeftOf="parent" 75 app:layout_constraintRight_toRightOf="parent"> 76 77 <LinearLayout 78 android:layout_width="match_parent" 79 android:layout_height="wrap_content" 80 android:orientation="vertical" 81 android:layout_marginTop="10dp" > 82 83 <TextView 84 android:id="@+id/peSearchText" 85 android:layout_width="match_parent" 86 android:layout_height="wrap_content" 87 android:text="@string/peSearchTitle" 88 android:gravity="center" 89 android:layout_marginTop="10dp" /> 90 91 <ScrollView 92 android:layout_width="match_parent" 93 android:layout_height="wrap_content" 94 android:layout_marginTop="15dp"> 95 96 <!-- Listの作成 --> 97 <LinearLayout 98 android:layout_width="match_parent" 99 android:layout_height="wrap_content" 100 android:orientation="vertical"> 101 <LinearLayout 102 android:layout_width="match_parent" 103 android:layout_height="wrap_content" 104 android:background="@drawable/border_bottom" 105 android:orientation="horizontal"> 106 107 <TextView 108 android:layout_width="wrap_content" 109 android:layout_height="60dp" 110 android:layout_marginLeft="25dp" 111 android:gravity="center" 112 android:text="@string/a" /> 113 114 <Spinner 115 android:id="@+id/aLine" 116 android:layout_width="match_parent" 117 android:layout_height="60dp" 118 android:layout_marginRight="25dp" 119 android:textAlignment="center"/> 120 </LinearLayout> 121 122 123 </LinearLayout> 124 </ScrollView> 125 </LinearLayout> 126 </FrameLayout>
上記のコードの<FraneLayout>部分を別のフラグメントにreplaceしたいのですがなぜか<FraneLayout>部分が残り、その上に重なってフラグメントが貼り付けられます。
replace(R.id.contentsArea,fragment)からreplace(R.id.tagSelectArea,fragment)に変更するとうまく動作します。
過去の質問を調べてもわからなかったので詳しい方がいらっしゃればぜひ教えてください。
お願いします
回答1件
あなたの回答
tips
プレビュー