1. 程式人生 > >已解決:ViewPager的notifyDataSetChanged不重新整理問題

已解決:ViewPager的notifyDataSetChanged不重新整理問題

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; } }