kotlin
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) //コンポーネントの取得 val tagSelectSpinner = view.findViewById<Spinner>(R.id.tagSelect) val button= view.findViewById<Button>(R.id.buttn) //Fragmentの取得 val fragment = Fragment() //Spinnerにセット(Tag) val tagList = arrayListOf("a","b","c") val adapter = context?.let { ArrayAdapter(it,android.R.layout.simple_spinner_item,tagList) } tagSelectSpinner.adapter = adapter //Spinnerを作成し値を設定 val spin = view.findViewById<Spinner>(R.id.aLine) val ar = arrayListOf("aaa") val adap = context?.let { ArrayAdapter(it,android.R.layout.simple_spinner_item,ar) } spin.adapter = adap //ボタンをクリック button.setOnClickListener { replaceFragment(fragment) } } private fun replaceFragment(fragment:Fragment){ val fragmanager = fragmentManager?.beginTransaction() fragmanager?.replace(R.id.contentsArea,fragment)?.commit() } }
xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".FragmentSearch"> <!-- Title --> <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" android:background="@color/teal_200" android:paddingLeft="20dp" android:paddingTop="5dp" android:paddingBottom="5dp" android:text="@string/app_name" android:textSize="40dp" /> <!-- 絞り込み検索 --> <!-- Tagの選択 --> <LinearLayout android:id="@+id/tagSelectArea" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" app:layout_constraintTop_toBottomOf="@+id/title" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" android:gravity="center" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="25dp" android:layout_weight="1" android:gravity="center" android:text="@string/tagText"/> <!-- SelectButton --> <Spinner android:id="@+id/tagSelect" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/tag_border" android:textAlignment="center" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="25dp" android:layout_marginLeft="25dp" android:backgroundTint="@color/tagItemColor" android:text="@string/selectButton" android:layout_weight="1" /> </LinearLayout> <!-- ここを別のフラグメントに切り替えたい --> <FrameLayout android:id="@+id/contentsArea" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toBottomOf="@+id/tagSelectArea" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginTop="10dp" > <TextView android:id="@+id/peSearchText" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/peSearchTitle" android:gravity="center" android:layout_marginTop="10dp" /> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp"> <!-- Listの作成 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/border_bottom" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="60dp" android:layout_marginLeft="25dp" android:gravity="center" android:text="@string/a" /> <Spinner android:id="@+id/aLine" android:layout_width="match_parent" android:layout_height="60dp" android:layout_marginRight="25dp" android:textAlignment="center"/> </LinearLayout> </LinearLayout> </ScrollView> </LinearLayout> </FrameLayout>
上記のコードの<FraneLayout>部分を別のフラグメントにreplaceしたいのですがなぜか<FraneLayout>部分が残り、その上に重なってフラグメントが貼り付けられます。
replace(R.id.contentsArea,fragment)からreplace(R.id.tagSelectArea,fragment)に変更するとうまく動作します。
過去の質問を調べてもわからなかったので詳しい方がいらっしゃればぜひ教えてください。
お願いします
まだ回答がついていません
会員登録して回答してみよう