已解決:ViewPager的notifyDataSetChanged不重新整理問題
阿新 • • 發佈:2019-02-09
1.ViewPager的介面卡必須繼承FragmentStatePagerAdapter才能重新整理。
2.要在destroyItem加摧毀View的程式碼
3.在getItemPosition方法中返回POSITION_NONE。
這樣,我們在呼叫mAdapter.notifyDataSetChanged()時才能重新整理
具體實現如下:
classMyAdapterextendsFragmentStatePagerAdapter{ privateArrayList<Fragment>returnFragment; publicMyAdapter(FragmentManager fm,ArrayList<Fragment> returnFragment) {
super(fm);
this.returnFragment= returnFragment;
}
@Override
publicFragment getItem(intposition) {
returnreturnFragment.get(position);
}
@Override
public void destroyItem(ViewGroup container,intposition, Object object) {
super.destroyItem(container, position, object);
container.removeView(returnFragment.get(position).getView());
}
@Override
public int getCount() {
Log.i("tian","getCount: "+returnFragment.size());
returnreturnFragment.size();
}
@Override
public int getItemPosition(Object object) {
returnPOSITION_NONE;
}
}
具體實現如下:
classMyAdapterextendsFragmentStatePagerAdapter{ privateArrayList<Fragment>returnFragment; publicMyAdapter(FragmentManager fm,ArrayList<Fragment> returnFragment) {