package com.tofly.ghrq_route.common.ui.adapter;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Amu on 2017/5/24.
 */

public class BaseFragmentAdapter extends FragmentPagerAdapter {

    protected List<Fragment> mFragmentList;

    protected String[] mTitles;

    public BaseFragmentAdapter(FragmentManager fm){
        this(fm, null, null);
    }

    public BaseFragmentAdapter(FragmentManager fm, List<Fragment> fragments, String[] titles){
        super(fm);
        if (fragments == null)
            fragments = new ArrayList<>();
        mFragmentList = fragments;
        mTitles = titles;
    }

    public void add(Fragment fragment){
        if (isEmpty()){
            mFragmentList = new ArrayList<>();
        }
        mFragmentList.add(fragment);
    }


    public boolean isEmpty() {
        return mFragmentList == null;

    }

    @Override
    public Fragment getItem(int position) {
        return isEmpty() ? null : mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return isEmpty() ? 0 : mFragmentList.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mTitles[position];
    }
}
