这是情景:
'clear()'可以删除所有项目,但随后会使用旧片段而不是新片段重新填充适配器。
提前感谢您的帮助。
public class MyFragmentAdapter extends FragmentStatePagerAdapter {
private ArrayList<MyFragment> mFragments;
...
@Override
public Fragment getItem(int pos) {
return mFragments.get(pos);
}
...
public void clear() {
mFragments.clear();
notifyDataSetChanged();
}
public void addItems(List<MyItem> items) {
for(MyItem it : items) {
mFragments.add(MyFragment.newInstance(it));
}
notifyDataSetChanged();
}
@Override
public int getItemPosition(Object object) {
MyFragment fi = (MyFragment) object;
int idx = mFragments.indexOf(fi);
if (idx >= 0) {
return POSITION_UNCHANGED;
} else {
return POSITION_NONE;
}
}
...
}
public class MyActivity extends FragmentActivity {
private ViewPager mPager;
private MyFragmentAdapter mAdapter;
...
private void resetItems(List<MyItem> items) {
mAdapter.clear();
mAdapter.addItems(items);
}
...
}