问题描述
我无法在 FragmentPagerAdapter 中重用片段.. 使用 destroyItem() 方法,它正在删除片段但仍然没有再次调用 getItem() ..只有 2-3 个图像所以我使用 FragmentPagerAdapter 而不是FragmentStatePagerAdapter..
I am not able to reuse fragment in FragmentPagerAdapter.. Using destroyItem() method, It is deleting the fragment but still does not called getItem() again..There are just 2-3 Images so I am using FragmentPagerAdapter Instead of 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 中,
And In FragmentActivity,
pager.setAdapter(new ExamplePagerAdapter(getSupportFragmentManager(), res2));
推荐答案
KISS 答案:
简单使用 FragmentStatePagerAdapter 而不是 FragmentPagerAdapter.
Simple use FragmentStatePagerAdapter instead of FragmentPagerAdapter.
我得到了答案..首先我想删除这个问题,因为我犯了一个非常愚蠢的错误,但这个答案将帮助面临同样问题的人,而不是 FragmentPagerAdapter
,使用 FragmentStatePagerAdapter.
I got the answer.. Firstly I thought to delete this question as I am doing a very silly mistake but this answer will help someone who is facing the same problem that Instead of FragmentPagerAdapter
, use FragmentStatePagerAdapter
.
正如@BlackHatSamurai 在评论中提到的:
As @BlackHatSamurai mentioned in the comment:
这样做的原因是因为 FragmentStatePagerAdapter
破坏了作为未使用的片段.FragmentPagerAdapter
没有.
这篇关于FragmentPagerAdapter getItem 未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!