package com.bonait.bnframework.modules.home.adapter;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentStatePagerAdapter;

import android.app.Activity;
import android.view.ViewGroup;

import com.bonait.bnframework.common.constant.ConfigName;
import com.qmuiteam.qmui.widget.QMUIViewPager;

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

/**
 * Created by LY on 2019/3/26.
 */
public class FragmentAdapter extends FragmentStatePagerAdapter {
    private List<Fragment> fragments = new ArrayList<>();

    public FragmentAdapter(FragmentManager fm,List<Fragment> _fragments) {
        super(fm);
        fragments=_fragments;
    }

    public QMUIViewPager pager;
    public void SetViewPage(QMUIViewPager _pager)
    {
        pager=_pager;
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    public void addFragment(Fragment fragment) {
        fragments.add(fragment);
    }

    /**
     *
     * @param position
     * @param fragment
     */
    public void replaceFragment(int position,Fragment fragment)
    {
        fragments.set(position,fragment);
        notifyDataSetChanged();

        pager.setAdapter(ConfigName.getInstance().fragmentAdapter);
        pager.setCurrentItem(position);
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        return super.instantiateItem(container, position);
    }
}
