ViewPager in Fragment is not working second time

2019-07-29 05:58发布

问题:

I am using a FrameLayout to display a fragment which contains a ViewPager. Sometimes I need to change to another fragment . Everything works fine until I want to get back to Fragment which contains ViewPager .

Here is the code I am using :

Fragment which contains ViewPager :

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val view : View = inflater!!.inflate(R.layout.main_quotes, null)
    val vpQuotes = view.findViewById<CustomViewPager>(R.id.vpQuotes)

    val vpAdapter = ViewPagerAdapter(activity.supportFragmentManager)
    vpQuotes.adapter = vpAdapter

    val vpPageChangeListener : ViewPager.OnPageChangeListener = object : ViewPager.OnPageChangeListener {
        override fun onPageScrollStateChanged(state: Int) {
        }

        override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
        }

        override fun onPageSelected(position: Int) {
            (activity as ChangeLikeButton).changeLikeButton(vpAdapter.getQuoteItem(Shared.currentViewPager.currentItem))
        }

    }

    vpQuotes.swipeDirection = CustomViewPager.SwipeDirection.RIGHT
    vpQuotes.addOnPageChangeListener(vpPageChangeListener)
    vpQuotes.setPageTransformer(true, ViewPagerTransformer())
    vpQuotes.post { vpPageChangeListener.onPageSelected(0) }

    return view
}

ViewPagerAdapter :

class ViewPagerAdapter(fm : FragmentManager) : CustomViewPagerAdapter(fm) {
override fun getQuoteItem(position: Int): QuoteItem {
    return quoteItemList[position]
}

override var quoteItemList: ArrayList<QuoteItem> = arrayListOf()
private val ALLOWED_PAGES = 100

override fun getItem(position: Int): Fragment {
    quoteItemList.add(position, Shared.database.getRandomData())
    val quoteFragment = QuoteFragment()
    quoteFragment.getItemFromAdapter(quoteItemList[position])
    return quoteFragment
}

override fun getCount(): Int {
    return ALLOWED_PAGES
}

}

CustomViewPagerAdapter is a class which contains a method to extract some data Here is the code :

abstract class CustomViewPagerAdapter(fm : FragmentManager) : FragmentPagerAdapter(fm) {

abstract var quoteItemList : ArrayList<QuoteItem>


abstract fun getQuoteItem(position: Int) : QuoteItem

And the error I get is :

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
                                                                         at java.util.ArrayList.get(ArrayList.java:411)
                                                                         at studio.monk.royalquotes.custom.CustomViewPagerAdapter.getQuoteItem(CustomViewPagerAdapter.kt:13)
                                                                         at studio.monk.royalquotes.fragments.MainFragment$onCreateView$vpPageChangeListener$1.onPageSelected(MainFragment.kt:44)
                                                                         at studio.monk.royalquotes.fragments.MainFragment$onCreateView$1.run(MainFragment.kt:55)
                                                                         at android.os.Handler.handleCallback(Handler.java:751)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                         at android.os.Looper.loop(Looper.java:154)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

Basically the program logic is like this : App start -> New Fragment(every works fine) -> onButtonClick change fragment -> onButtonClick -> New Fragment with the same Viewpager(now I get the error)

I don't save any data on Fragment destroy to use it on next fragment but I still get this error . What should I do ?

回答1:

Actually, it would work if you change:

 val vpAdapter = ViewPagerAdapter(activity.supportFragmentManager)

to

 val vpAdapter = ViewPagerAdapter(childFragmentManager)


回答2:

I had the same issue. Changing the parent class of my PageAdapter from android.support.v4.app.FragmentPagerAdapter to android.support.v4.app.FragmentStatePagerAdapter solve my ViewPager display issue on "second time"!