gpt4 book ai didi

java - 旋转屏幕后, View 寻呼机保留旧 fragment

转载 作者:太空宇宙 更新时间:2023-11-04 11:39:15 25 4
gpt4 key购买 nike

我有一个 View 寻呼机,有 4 个 fragment 。 (我使用 FragmentStatePagerAdapter)

每个 fragment 都有一个“FrameLayout/容器”,我在其中添加和替换许多 fragment 。

一切正常,但是当我更改屏幕方向时,该行的第一个 fragment 将恢复到实际 fragment 上。然后两者同时出现。

我用一张图片来说明所发生的情况:

First Fragment

Second Fragment

[当我旋转屏幕时]

/image/kxaVn.png 

我的主要 Activity 代码

class MainActivity : AppCompatActivity() {


val numPages = 4
var pager: ViewPager? = null
private val TITLES = arrayOf("Feed", "Catalogo","Guia","Rendimento")
var menuImages:Array<ImageView>?=null
var menuTexts:Array<TextView>?=null

var fragments = arrayListOf<Fragment>()


private var fragmentCreated1: FeedContainerFragment? = null
private var fragmentCreated2: CatalogContainerFragment? = null
private var fragmentCreated3: GuideContainerFragment? = null
private var fragmentCreated4: TestContainerFragment? = null



override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_default)


fragments.add(FeedContainerFragment())
fragments.add(CatalogContainerFragment())
fragments.add(GuideContainerFragment())
fragments.add(TestContainerFragment())


menuImages= arrayOf(findViewById(R.id.icon_feed) as ImageView,
findViewById(R.id.icon_catalogue) as ImageView,
findViewById(R.id.icon_guide) as ImageView,
findViewById(R.id.icon_form) as ImageView)

menuTexts= arrayOf(findViewById(R.id.text_feed) as TextView,
findViewById(R.id.text_catalogue) as TextView,
findViewById(R.id.text_guide) as TextView,
findViewById(R.id.text_form) as TextView)




menuImages?.get(0)?.setColorFilter(ContextCompat.getColor(baseContext,R.color.colorAccent))
menuTexts?.get(0)?.setTextColor(ContextCompat.getColor(baseContext,R.color.colorAccent))


//view pager

pager = findViewById(R.id.pager) as ViewPager
val pagerAdapter = ScreenSlidePagerAdapter(supportFragmentManager)


pager?.adapter = pagerAdapter
pager?.offscreenPageLimit = 4


pager?.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

}

override fun onPageSelected(position: Int) {
pager?.adapter?.notifyDataSetChanged()
repaintMenuDefaultColor()

menuImages?.get(position)?.setColorFilter(ContextCompat.getColor(baseContext,R.color.colorAccent))
menuTexts?.get(position)?.setTextColor(ContextCompat.getColor(baseContext,R.color.colorAccent))

}

override fun onPageScrollStateChanged(state: Int) {

}
})


//Navigation Menus
MenuUtils.generateMaterialMenu(this)
MenuUtils.generateBottomMenu(this, this.pager!!)


}

private inner class ScreenSlidePagerAdapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm) {
override fun getCount(): Int {
return numPages
}

override fun getItem(position: Int): Fragment {
when(position) {
0->return FeedContainerFragment()
1->return CatalogContainerFragment()
2->return GuideContainerFragment()
3->return TestContainerFragment()

else->return FeedContainerFragment()
}

}


override fun getPageTitle(position: Int): CharSequence {
return TITLES[position]
}

override fun getItemPosition(obj: Any?): Int {

if (obj is Refreshable) {
return POSITION_NONE
}
return super.getItemPosition(obj)
}



override fun instantiateItem(container:ViewGroup, position:Int):Any {
val createdFragment = super.instantiateItem(container, position) as Fragment
when (position) {
0 -> fragmentCreated1 = createdFragment as FeedContainerFragment
1 -> fragmentCreated2 = createdFragment as CatalogContainerFragment
2 -> fragmentCreated3 = createdFragment as GuideContainerFragment
3 -> fragmentCreated4 = createdFragment as TestContainerFragment

}


return createdFragment
}

}


override fun onBackPressed() {
if (fragmentManager.backStackEntryCount > 0) {
fragmentManager.popBackStack()
} else {
super.onBackPressed()
}
}


fun repaintMenuDefaultColor(){
this.menuImages?.map {
it.setColorFilter(ContextCompat.getColor(baseContext,R.color.menu_icon))
}

this.menuTexts?.map {
it.setTextColor(ContextCompat.getColor(baseContext,R.color.menu_text))
}


}

}

欢迎任何帮助!

编辑:实际上,发生的情况是 ViewPager 将实际 fragment 保留为“幽灵”,然后恢复该行的第一个 fragment 。

有没有办法清洁屏幕并删除“幽灵”?

编辑2:找到解决方案。在将 fragment 添加到每个“容器”上之前,我需要验证 fragment 实例是否已存在问题不在 ViewPager 或 Adapter 上。每次我改变方向时都会包含一个新的 fragment 。

所以,只需将验证放在下面即可:

     if(savedInstanceState == null) {
activity
.supportFragmentManager
.beginTransaction()
.add(R.id.containerGuide, YourFragment())
.commit()
}

最佳答案

旋转屏幕时,将重新创建 Activity 和 Fragments,以便从位置 0 开始..

  1. 重建 viewpager 时保存 Fragment 位置只需选择您需要的 Fragment。
  2. 查看此链接 https://developer.android.com/guide/topics/resources/runtime-changes.html
  3. 禁用屏幕旋转。

关于java - 旋转屏幕后, View 寻呼机保留旧 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42959810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com