问题描述
我不能够重用片段FragmentPagerAdapter ..使用destroyItem()方法,它是删除片段,但仍不能称之为getItem()时again..There只是2-3图像,所以我使用而不是FragmentPagerAdapter FragmentStatePagerAdapter ..
公共类ExamplePagerAdapter扩展FragmentPagerAdapter {
ArrayList的<字符串>网址;
INT大小= 0;
公共ExamplePagerAdapter(FragmentManager FM,ArrayList的<字符串>水库){
超(FM);
网址=资源;
大小= urls.size();
}
@覆盖
公众诠释getCount将(){
如果(网址== NULL)
{
返回0;
}
其他
{
返回的大小;
}
}
@覆盖
公共无效destroyItem(ViewGroup中的容器,INT位置,Object对象){
FragmentManager经理=((片段)对象).getFragmentManager();
FragmentTransaction反式= manager.beginTransaction();
trans.remove((片段)的对象);
trans.commit();
}
@覆盖
公共片段的getItem(INT位置){
片段片段=新FloorPlanFragment();
叠B =新包();
b.putInt(P,位);
b.putString(形象,urls.get(位置));
Log.i(图像,+ urls.get(位置));
fragment.setArguments(B);
返回片段;
}
}
}
和在FragmentActivity,
pager.setAdapter(新ExamplePagerAdapter(getSupportFragmentManager(),RES2));
KISS答案
简单的使用 FragmentStatePagerAdapter
而不是 FragmentPagerAdapter
我得到了答案..首先我想删除这个问题,因为我做了非常愚蠢的错误,但这个答案会帮助别人谁也面临同样的问题,而不是 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;
}
}
}
And In FragmentActivity,
pager.setAdapter(new ExamplePagerAdapter(getSupportFragmentManager(), res2));
KISS Answer
Simple use FragmentStatePagerAdapter
instead of FragmentPagerAdapter
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
..
这篇关于FragmentPagerAdapter的getItem不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!