Fragment中的ViewPager第二次无法正常工作

Fragment中的ViewPager第二次无法正常工作

本文介绍了Fragment中的ViewPager第二次无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FrameLayout来显示包含ViewPager的片段.有时我需要换另一个片段.一切正常,直到我想回到包含ViewPager的Fragment.

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 :

包含ViewPager的片段:

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:

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是一个类,其中包含一种提取某些数据的方法这是代码:

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

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

abstract var quoteItemList : ArrayList<QuoteItem>


abstract fun getQuoteItem(position: Int) : QuoteItem

我得到的错误是:

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)

基本上,程序逻辑是这样的:应用开始-> New Fragment(一切正常)-> onButtonClick更改片段-> onButtonClick->具有相同Viewpager的Ne​​w Fragment(现在我得到了错误)

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)

我没有在Fragment destroy上保存任何数据以在下一个fragment上使用它,但是仍然出现此错误.我该怎么办?

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 ?

推荐答案

实际上,如果您进行以下更改,它将起作用:

Actually, it would work if you change:

 val vpAdapter = ViewPagerAdapter(activity.supportFragmentManager)

 val vpAdapter = ViewPagerAdapter(childFragmentManager)

这篇关于Fragment中的ViewPager第二次无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 03:58