开始使用的时候 fragment的动态删除刷新总有问题
。后来请教得知
PagerAdapter的 notifyDataSetChanged() 需要继承 FragmentStatePagerAdapter 才可以
另外还需要重写 getItemPosition 方法
PagerAdapter
public class TabFragmentPagerAdapter extends FragmentStatePagerAdapter {
private FragmentManager mfragmentManager;
public TabFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
//
@Override
public int getItemPosition(@NonNull Object object) {
return PagerAdapter.POSITION_NONE;
}
@Override
public Fragment getItem(int arg0) {
return list_fragment.get(arg0);//显示第几个页面
}
@Override
public int getCount() {
return list_fragment.size();//有几个页面
}
}
viewpager 监听
public class MyPagerChangeListener implements ViewPager.OnPageChangeListener {
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int arg0) {
// setMyViewPager(arg0);//初始化tob
pos_now=arg0;
}
}
添加fragment到 viewpager
myViewPager.setOnPageChangeListener(new MyPagerChangeListener()); //把Fragment添加到List集合里面
list_fragment = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
ItmeFragment_ErrorSubject itmeFragment = new ItmeFragment_ErrorSubject();
Bundle bundle = new Bundle();
bundle.putInt("pos", i);
itmeFragment.setArguments(bundle);
list_fragment.add(itmeFragment);
}
adapter = new TabFragmentPagerAdapter(getSupportFragmentManager());
myViewPager.setAdapter(adapter);
刷新处理
list.remove(pos_now);
list_fragment.remove(pos_now);
tvTotal.setText(list.size() + "");
adapter.notifyDataSetChanged();