我有一个简单的FragmentPagerAdapter:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
pagerAdapterList = new ArrayList<>();
super(fm);
}
@Override
public Fragment getItem(int position) {
PagerAdapter fragment;
if (position > pagerAdapterList.size() - 1) {
fragment = new PagerAdapter(followRequests.get(position));
pagerAdapterList.add(fragment);
} else
return pagerAdapterList.get(position);
return fragment;
}
public int removeView (ViewPager pager, int position)
{
pager.setAdapter (null);
pagerAdapterList.remove (position);
data.remove (position);
pager.setAdapter (this);
return position;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
getItem(position);
return super.instantiateItem(container, position);
}
@Override
public int getCount() {
return data.size();
}
}
和PagerAdapter类:
public class PagerAdapter extends Fragment {
Data data;
private View overlayView;
public PagerAdapter() {
}
public PagerAdapter(Data data) {
this.data= data;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.notification_header_fragment_pager, container, false);
TextView id_txtView = (TextView)
overlayView = (View) rootView.findViewById(R.id.viewOverlay);
//bind some data for example id_txtView.setText(data.getText());
return rootView;
}
public void showOverlayView() {
if(overlayView != null)
overlayView.setVisibility(View.VISIBLE);
}
}
首先我打电话
removeView(mViewPager,position);
然后我获得新数据,然后调用
adapter.notifyDataSetChanged();
一切正常,我可以在PagerAdapter中绑定新数据并在View Pager上显示。
但是当我打电话时
((PagerAdapter)adapter.getItem(mViewPager.getCurrentItem()))。showOverlayView();
我得到当前片段而没有任何对其ui组件的引用(如overlayView),它们都是null !!
我错了什么? 请帮帮我,谢谢。