旋转屏幕后,查看寻呼机保存旧片段

时间:2017-03-22 18:21:43

标签: java android android-fragments android-viewpager

我有一个视图寻呼机,有4个片段。 (我正在使用FragmentStatePagerAdapter)

每个片段都有一个" FrameLayout / container",其中我添加并替换了许多片段。

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

我用一个例子说明了发生了什么:

First Fragment

Second Fragment

[当我旋转屏幕]

http://i.stack.imgur.com/kxaVn.png 

我的主要活动代码

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将实际片段保持为&#34; Ghost&#34;然后恢复该行的第一个片段。

有没有办法清理屏幕并移除&#34; Ghost&#34;吗

编辑2:找到解决方案。我需要验证片段实例是否已经存在,然后在每个&#34;容器上添加片段&#34; 问题不在ViewPager或Adapter上。每当我改变方向时,都会包含一个新的片段。

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

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

1 个答案:

答案 0 :(得分:0)

When Rotating Screen the Activity and the Fragments are recreated so its start from position 0..

  1. Save Fragment position when viewpager is rebuild just choose the Fragment you need.
  2. Check out this link https://developer.android.com/guide/topics/resources/runtime-changes.html
  3. Disable screen Rotations.