public class FragmentAdapter extends FragmentPagerAdapter {
private FragmentManager fragmentManager;
private List<Fragment> fragmentList;
public FragmentAdapter(FragmentManager fm, FragmentManager fragmentManager) {
super(fm);
this.fragmentManager = fragmentManager;
}
public FragmentAdapter(FragmentManager fm, List<Fragment> fragmentList) {
super(fm);
this.fragmentManager = fm;
this.fragmentList = fragmentList;
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
fragment = fragmentList.get(position);
Bundle bundle = new Bundle();
bundle.putString("id", position + "");
fragment.setArguments(bundle);
return fragment;
}
@Override
public int getCount() {
return fragmentList.size();
}
@NonNull
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
fragmentManager.beginTransaction().show(fragment).commit();
return fragment;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
Fragment fragment = fragmentList.get(position);
fragmentManager.beginTransaction().hide(fragment).commit();
}
}
网友评论