package com.zxhl.cms.common


import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter

import com.zxhl.cms.common.base.BaseFragment
import java.util.*

/**
 * Created by gaoleichao on 2018/10/11.
 */
class FragmentAdapter : FragmentPagerAdapter {
    private val mCateInfoBase = ArrayList<BaseFragment>()
    private val mTitleList = ArrayList<String>()

    constructor(fm: FragmentManager?) : super(fm!!)

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

    fun addFrag(fragment: BaseFragment?) {
        if (fragment != null) {
            mCateInfoBase.add(fragment)
        }
    }

    fun addFrag(fragment: BaseFragment?, title: String) {
        if (fragment != null) {
            mCateInfoBase.add(fragment)
            mTitleList.add(title)
        }
    }

    fun clear() {
        mCateInfoBase.clear()
    }


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

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

    override fun getItemId(position: Int): Long {
        // 获取当前数据的hashCode
        val hashCode: Int = mCateInfoBase.get(position).hashCode()
        return hashCode.toLong()
    }
    override fun destroyItem(container: ViewGroup, position: Int, obj: Any) {
        //如果注释这行，那么不管怎么切换，page都不会被销毁
//        super.destroyItem(container, position, obj)
    }

}