package com.cheng.blzb.ui.base

import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter

class BasePageAdapter(
    private val fm: FragmentManager,
    private val titles: List<String>,
    private val mList: MutableList<Fragment>,
    behavior: Int = BEHAVIOR_SET_USER_VISIBLE_HINT
) : FragmentStatePagerAdapter(fm, behavior) {

    override fun getItem(position: Int): Fragment {
        return mList[position]
    }

    override fun getCount(): Int {
        return mList.size
    }

    override fun getPageTitle(position: Int): CharSequence? {
        if (titles.isEmpty()) return null
        return titles[position]
    }

    override fun instantiateItem(container: View, position: Int): Any {
        val fragment = super.instantiateItem(container, position) as Fragment
        this.fm.beginTransaction().show(fragment).commit()
        return fragment
    }

    override fun destroyItem(container: View, position: Int, `object`: Any) {
        val fragment = mList[position]
        fm.beginTransaction().hide(fragment).commitAllowingStateLoss()
    }

    override fun getItemPosition(`object`: Any): Int {
        return POSITION_NONE
    }
}