public class MainPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragmentList=new ArrayList<>();
private List<String> fragmentTitle=new ArrayList<>();
public MainPagerAdapter(@NonNull FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return fragmentList.size();
}
@NonNull
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
public void addFragment(Fragment fragment,String title){
fragmentList.add(fragment);
fragmentTitle.add(title);
}
/**
* 設置ViewPager的標題,可能之後可以寫一些判斷像是現在在哪個fragment之用
*/
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return fragmentTitle.get(position);
}
}