我正在使用FrameLayout来显示包含ViewPager的片段。有时我需要换成另一个片段。一切正常,直到我想回到包含ViewPager的Fragment。
以下是我正在使用的代码:
包含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是一个包含提取某些数据的方法的类 这是代码:
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)
基本上程序逻辑是这样的: 应用开始 - &gt;新片段(每件作品都很好) - &gt; onButtonClick更改片段 - &gt; onButtonClick - &gt;具有相同Viewpager的新片段(现在我收到错误)
我没有在Fragment destroy上保存任何数据以在下一个片段上使用它,但我仍然会收到此错误。我该怎么办?
答案 0 :(得分:2)
实际上,如果您进行以下更改,它将起作用:
val vpAdapter = ViewPagerAdapter(activity.supportFragmentManager)
到
val vpAdapter = ViewPagerAdapter(childFragmentManager)
答案 1 :(得分:0)
我有同样的问题。将我的PageAdapter
的父类从android.support.v4.app.FragmentPagerAdapter
更改为android.support.v4.app.FragmentStatePagerAdapter
解决了我的ViewPager显示问题“第二次”!