android / views-widgets-samples

Multiple samples showing the best practices in views-widgets on Android.
Apache License 2.0
5.03k stars 3.01k forks source link

Viewpager2 Adding item at 0th position gives issue #263

Closed nawanirakshit closed 3 weeks ago

nawanirakshit commented 3 weeks ago

I am trying to add an item at the 0th position, but after adding the item, the

The code looks like the following for the adapter.

    inner class ScreenSlidePagerAdapter(fa: FragmentActivity) : FragmentStateAdapter(fa) {
        override fun getItemCount(): Int {
            return list.size
        }

        override fun createFragment(position: Int): Fragment = ScrollFragment(list[position])

        fun addFragment(string: String) {
            list.add(string)
            notifyItemInserted(list.size)
        }

        fun addAtPreviousFragment(string: String) {
            list.add(0, string)
            notifyItemInserted(0)
        }
    }

Captured a screenshot for the same https://drive.google.com/file/d/19-jY-xKx8IYXH2pk3-p0qtUPZJPavyZy/view

Sample code repository https://github.com/nawanirakshit/ViewPagerCheck