/**
* Created by admin on 2016/12/5.
* 针对vpg改变fragments的数量后刷新是没用的问题
*/
public class SpotAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> fragments;
public SpotAdapter(FragmentManager fm, ArrayList<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
// TODO: 2020/3/31 主要就下面2个,hashCode的唯一性保证中途替换新的fragment不会复用之前的旧布局
@Override
public long getItemId(int position) {
return fragments.get(position).hashCode();
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
替换的时候更新数据
/**
* 中途改变list<Fragment>本来就挺无奈的,但难免会遇到这种要求
* @param list_bean 新的列表
*/
private void updateVpgData(List<CourseTeacherListBean.DataBean> list_bean) {
//新列表数量比之前的多,在之前的基础上加
if (list_bean.size() > list_frag.size()) {
for (int i = list_frag.size(); i < list_bean.size(); i++) {
CourseListFrag courseListFrag = new CourseListFrag();
list_frag.add(courseListFrag);
}
} else if (list_bean.size() < list_frag.size()) {
ArrayList<Fragment> list = new ArrayList<>();
for (int i = list_bean.size(); i < list_frag.size(); i++) {
list.add(list_frag.get(i));
}
list_frag.removeAll(list);
}
vpgFAdapter.notifyDataSetChanged();
vpg.setCurrentItem(0);//跳到第几页随意,被越位就行
// 重新更新一遍数据
for (int i = 0; i < list_frag.size(); i++) {
CourseListFrag courseListFrag = (CourseListFrag) list_frag.get(i);
courseListFrag.init(typeString, String.valueOf(list_bean.get(i).getId()));
}
}
网友评论