我无法在FragmentPagerAdapter中重用片段。使用destroyItem()方法,它正在删除片段,但仍然不会再次调用getItem()。.只有2-3张图像,所以我使用FragmentPagerAdapter而不是FragmentStatePagerAdapter。
public class ExamplePagerAdapter extends FragmentPagerAdapter {
ArrayList < String > urls;
int size = 0;
public ExamplePagerAdapter(FragmentManager fm, ArrayList < String > res) {
super(fm);
urls = res;
size = urls.size();
}
@Override
public int getCount() {
if (urls == null) {
return 0;
} else {
return size;
}
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
FragmentManager manager = ((Fragment) object).getFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.remove((Fragment) object);
trans.commit();
}
@Override
public Fragment getItem(int position) {
Fragment fragment = new FloorPlanFragment();
Bundle b = new Bundle();
b.putInt("p", position);
b.putString("image", urls.get(position));
Log.i("image", "" + urls.get(position));
fragment.setArguments(b);
return fragment;
}
}
在FragmentActivity中
pager.setAdapter(new ExamplePagerAdapter(getSupportFragmentManager(), res2));
destroyItem()
吗?那是没有必要的。