結構恥ずかしいコードですが、一応動いたので載せておきます。
なお、build.gradle の dependencies に以下の追加が必要でした。
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation "androidx.fragment:fragment-ktx:1.3.5"
ViewPager には fragment4 と fragment2 を載せてスワイプさせ、 fragment4 に fragment1 か fragment3 を載せます。
ViewModel の func1_mode が 1 なら fragment1, 3 なら fragment3 を表示するとし、fragment1 のボタンが押されたら Fragment1 が func1_mode を 3 にしています。
MainActivity.kt
kotlin
1import androidx.appcompat.app.AppCompatActivity
2import android.os.Bundle
3import androidx.fragment.app.Fragment
4import androidx.lifecycle.ViewModelProviders
5import kotlinx.android.synthetic.main.activity_main.*
6
7class MainActivity : AppCompatActivity() {
8 override fun onCreate(savedInstanceState: Bundle?) {
9 super.onCreate(savedInstanceState)
10 setContentView(R.layout.activity_main)
11
12 var viewModel = ViewModelProviders.of(this).get(SampleViewModel::class.java)
13
14 val fragmentList = arrayListOf<Fragment>(
15 Fragment4(),
16 Fragment2(),
17 )
18
19 /// adapterのインスタンス生成
20 val adapter = SamplePagerAdapter(supportFragmentManager, fragmentList) /// adapterをセット
21 viewPager.adapter = adapter
22 }
23}
SamplePagerAdapter.kt
kotlin
1import androidx.fragment.app.Fragment
2import androidx.fragment.app.FragmentManager
3import androidx.fragment.app.FragmentStatePagerAdapter
4
5class SamplePagerAdapter(fm: FragmentManager, private val fragmentList: List<Fragment>) :
6 FragmentStatePagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
7
8 // 表示するフラグメントを制御する
9 override fun getItem(position: Int): Fragment {
10 return fragmentList[position]
11 }
12
13 // viewPagerにセットするコンテンツ(フラグメントリスト)のサイズ
14 override fun getCount(): Int {
15 return fragmentList.size
16 }
17}
SampleViewModel.kt
kotlin
1import androidx.lifecycle.MutableLiveData
2import androidx.lifecycle.ViewModel
3
4class SampleViewModel : ViewModel() {
5 val func1_mode = MutableLiveData<Int>(1)
6
7 val func1_text = MutableLiveData<String>("dummy")
8}
Fragment1.kt
kotlin
1import android.os.Bundle
2import android.view.LayoutInflater
3import android.view.View
4import android.view.ViewGroup
5import androidx.fragment.app.Fragment
6import androidx.fragment.app.activityViewModels
7import kotlinx.android.synthetic.main.fragment1.*
8
9class Fragment1 : Fragment() {
10 override fun onCreateView(
11 inflater: LayoutInflater, container: ViewGroup?,
12 savedInstanceState: Bundle?
13 ): View? {
14 return inflater.inflate(R.layout.fragment1, container, false)
15 }
16
17 private val viewModel: SampleViewModel by activityViewModels()
18
19 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
20 super.onViewCreated(view, savedInstanceState)
21
22 first_button.setOnClickListener {
23 viewModel.func1_mode.value = 3
24 }
25 viewModel.func1_text.observe(viewLifecycleOwner) { text ->
26 func1_text.text = text
27 }
28 }
29}
Fragment2.kt
kotlin
1import android.os.Bundle
2import android.view.LayoutInflater
3import android.view.View
4import android.view.ViewGroup
5import androidx.fragment.app.Fragment
6
7class Fragment2 : Fragment() {
8 override fun onCreateView(
9 inflater: LayoutInflater, container: ViewGroup?,
10 savedInstanceState: Bundle?
11 ): View? {
12 return inflater.inflate(R.layout.fragment2, container, false)
13 }
14}
Fragment3.kt
kotlin
1import android.os.Bundle
2import android.view.LayoutInflater
3import android.view.View
4import android.view.ViewGroup
5import androidx.fragment.app.Fragment
6import androidx.fragment.app.activityViewModels
7import kotlinx.android.synthetic.main.fragment3.*
8
9class Fragment3 : Fragment() {
10 override fun onCreateView(
11 inflater: LayoutInflater, container: ViewGroup?,
12 savedInstanceState: Bundle?
13 ): View? {
14 return inflater.inflate(R.layout.fragment3, container, false)
15 }
16
17 private val viewModel: SampleViewModel by activityViewModels()
18
19 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
20 super.onViewCreated(view, savedInstanceState)
21
22 second_button.setOnClickListener {
23 viewModel.func1_text.value = func1_edit.text.toString()
24 viewModel.func1_mode.value = 1
25 }
26 }
27}
Fragment4.kt
kotlin
1import android.os.Bundle
2import android.view.LayoutInflater
3import android.view.View
4import android.view.ViewGroup
5import androidx.fragment.app.Fragment
6import androidx.fragment.app.activityViewModels
7
8class Fragment4 : Fragment() {
9 override fun onCreateView(
10 inflater: LayoutInflater, container: ViewGroup?,
11 savedInstanceState: Bundle?
12 ): View? {
13 return inflater.inflate(R.layout.fragment4, container, false)
14 }
15
16 private val model: SampleViewModel by activityViewModels()
17 private var fragment1 = Fragment1()
18 private var fragment3 = Fragment3()
19
20 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
21 super.onViewCreated(view, savedInstanceState)
22
23 model.func1_mode.observe(viewLifecycleOwner) { mode ->
24 var f:Fragment = fragment1
25 if(mode != 1) f = fragment3
26 childFragmentManager.beginTransaction().replace(R.id.function1_container, f).commit()
27 }
28 }
29}
以下レイアウト
activity_main.xml
xml
1<?xml version="1.0" encoding="utf-8"?>
2<RelativeLayout 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=".MainActivity"
8 android:background="#f9f0e2">
9
10 <androidx.viewpager.widget.ViewPager
11 android:id="@+id/viewPager"
12 android:layout_width="match_parent"
13 android:layout_height="match_parent"
14 app:layout_constraintBottom_toBottomOf="parent"
15 app:layout_constraintEnd_toEndOf="parent"
16 app:layout_constraintStart_toStartOf="parent"
17 app:layout_constraintTop_toTopOf="parent" />
18</RelativeLayout>
fragment1.xml
xml
1<?xml version="1.0" encoding="utf-8"?>
2<androidx.constraintlayout.widget.ConstraintLayout
3 xmlns:android="http://schemas.android.com/apk/res/android"
4 xmlns:app="http://schemas.android.com/apk/res-auto"
5 xmlns:tools="http://schemas.android.com/tools"
6 android:layout_width="match_parent"
7 android:layout_height="match_parent"
8 tools:context=".Fragment1">
9
10 <Button
11 android:id="@+id/first_button"
12 android:layout_width="wrap_content"
13 android:layout_height="wrap_content"
14 android:text="to Fragment3"
15 app:layout_constraintBottom_toBottomOf="parent"
16 app:layout_constraintEnd_toEndOf="parent"
17 app:layout_constraintStart_toStartOf="parent"
18 app:layout_constraintTop_toBottomOf="@id/func1_text" />
19
20 <TextView
21 android:id="@+id/func1_text"
22 android:layout_width="0dp"
23 android:layout_height="wrap_content"
24 android:layout_marginStart="16dp"
25 android:layout_marginEnd="16dp"
26 app:layout_constraintBottom_toBottomOf="parent"
27 app:layout_constraintEnd_toEndOf="parent"
28 app:layout_constraintStart_toStartOf="parent"
29 app:layout_constraintTop_toTopOf="parent" />
30
31</androidx.constraintlayout.widget.ConstraintLayout>
fragment2.xml
xml
1<?xml version="1.0" encoding="utf-8"?>
2<androidx.constraintlayout.widget.ConstraintLayout
3 xmlns:android="http://schemas.android.com/apk/res/android"
4 xmlns:tools="http://schemas.android.com/tools"
5 android:layout_width="match_parent"
6 android:layout_height="match_parent"
7 tools:context=".Fragment2">
8 <TextView
9 android:layout_width="match_parent"
10 android:layout_height="match_parent"
11 android:text="Fragment2" />
12</androidx.constraintlayout.widget.ConstraintLayout>
fragment3.xml
xml
1<?xml version="1.0" encoding="utf-8"?>
2<androidx.constraintlayout.widget.ConstraintLayout
3 xmlns:android="http://schemas.android.com/apk/res/android"
4 xmlns:app="http://schemas.android.com/apk/res-auto"
5 xmlns:tools="http://schemas.android.com/tools"
6 android:layout_width="match_parent"
7 android:layout_height="match_parent"
8 tools:context=".Fragment3">
9
10 <EditText
11 android:id="@+id/func1_edit"
12 android:layout_width="0dp"
13 android:layout_height="wrap_content"
14 android:layout_marginStart="16dp"
15 android:layout_marginEnd="16dp"
16 android:ems="10"
17 android:text="Fragment3"
18 app:layout_constraintBottom_toBottomOf="parent"
19 app:layout_constraintEnd_toEndOf="parent"
20 app:layout_constraintStart_toStartOf="parent"
21 app:layout_constraintTop_toTopOf="parent" />
22 <Button
23 android:id="@+id/second_button"
24 android:layout_width="wrap_content"
25 android:layout_height="wrap_content"
26 android:text="set Text"
27 app:layout_constraintBottom_toBottomOf="parent"
28 app:layout_constraintEnd_toEndOf="parent"
29 app:layout_constraintStart_toStartOf="parent"
30 app:layout_constraintTop_toBottomOf="@id/func1_edit" />
31 <TextView
32 android:id="@+id/textView"
33 android:layout_width="wrap_content"
34 android:layout_height="wrap_content"
35 android:text="Input Text:"
36 app:layout_constraintStart_toStartOf="@id/func1_edit"
37 app:layout_constraintBottom_toTopOf="@id/func1_edit" />
38</androidx.constraintlayout.widget.ConstraintLayout>
fragment4.xml
xml
1<?xml version="1.0" encoding="utf-8"?>
2<androidx.constraintlayout.widget.ConstraintLayout
3 xmlns:android="http://schemas.android.com/apk/res/android"
4 xmlns:tools="http://schemas.android.com/tools"
5 android:layout_width="match_parent"
6 android:layout_height="match_parent"
7 android:id="@+id/function1_container"
8 tools:context=".Fragment4">
9</androidx.constraintlayout.widget.ConstraintLayout>