'ViewPager2 fails on screen rotation
I am novice to android and try to make ViewPager2 working with different fragments having different layouts. My sample code has simplest fragments that differ only in background color. ViewModel is not used. The code works ok in one orientation, but when it changes it doesn't work and shows only one fragment, but creating its view multiple times. I wonder why it is.. Thanks in advance for any help and suggestions!
abstract class SampleSliderActivity : FragmentActivity() {
private lateinit var viewPager: ViewPager2
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_my_slider)
viewPager = findViewById(R.id.viewPager)
viewPager.adapter = createViewPagerAdapter()
}
abstract fun createViewPagerAdapter(): RecyclerView.Adapter<*>
//val items: ItemsViewModel by viewModels()
}
class SampleMutableCollectionFragmentActivity : SampleSliderActivity() {
override fun createViewPagerAdapter(): RecyclerView.Adapter<*> {
val TAG: String="HeccjNehbcnj"
Log.d(TAG, "create adapter")
return object : FragmentStateAdapter(this) {
override fun createFragment(position: Int): SamplePageFragment {
Log.d(TAG, "create fragment #: " + position)
SamplePageFragment.position = position
return SamplePageFragment()
}
override fun getItemCount(): Int = 4
}
}
}
class SamplePageFragment : Fragment() {
private val pageResource = arrayOf(
R.layout.fragment_my_settings_1, R.layout.fragment_my_settings_2, R.layout.fragment_my_settings_3, R.layout.fragment_my_settings_4)
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(pageResource[position], container, false)
val TAG: String="HeccjNehbcnj"
Log.d(TAG, "on create view :" + position)
/*
when (position) {
0 -> {
// do something
}
}
*/
return view
}
companion object {
var position: Int = 0
}
}
and my layout for ViewPager2 activity_my_slider.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
And fragments-
fragment_my_settings_1.xml,
fragment_my_settings_2.xml,
fragment_my_settings_3.xml,
fragment_my_settings_4.xml
they only differ in background
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF1220FF" <!-- it changes -->
android:orientation="vertical"
android:paddingTop="8dp">
<TextView
android:id="@+id/name_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:textSize="50sp"
android:gravity="center" />
</LinearLayout>
Its output on swiping:
2022-05-08 12:02:40.111 23822-23822/D/HeccjNehbcnj: create adapter
2022-05-08 12:02:40.179 23822-23822/D/HeccjNehbcnj: create fragment #: 0
2022-05-08 12:02:40.208 23822-23822/D/HeccjNehbcnj: on create view :0
2022-05-08 12:02:44.907 23822-23822/D/HeccjNehbcnj: create fragment #: 1
2022-05-08 12:02:44.971 23822-23822/D/HeccjNehbcnj: on create view :1
2022-05-08 12:02:45.030 23822-23822/D/HeccjNehbcnj: create fragment #: 2
2022-05-08 12:02:47.152 23822-23822/D/HeccjNehbcnj: on create view :2
2022-05-08 12:02:47.180 23822-23822/D/HeccjNehbcnj: create fragment #: 3
2022-05-08 12:02:49.247 23822-23822/D/HeccjNehbcnj: on create view :3
but when screen orientation changes I get this in loop and no new view is shown, swipe stops to work:
2022-05-08 12:06:43.317 23822-23822/D/HeccjNehbcnj: create adapter
2022-05-08 12:06:43.333 23822-23822/D/HeccjNehbcnj: on create view :3
2022-05-08 12:06:43.339 23822-23822/D/HeccjNehbcnj: on create view :3
2022-05-08 12:06:43.344 23822-23822/D/HeccjNehbcnj: on create view :3
2022-05-08 12:06:43.351 23822-23822/D/HeccjNehbcnj: on create view :3
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
