发现无论是FragmentPagerAdapter还是FragmentStatePagerAdapter 当容器内的fragment超过4个就会产生回收问题(这是我实际遇到的)。
所以最经典的Tablayout+ViewPager+Fragment的结构,当点击第四个Tab后,第二个Tab里的Fragment 就会走 onDestroyView()方法,回收掉View。解决方法如下:
@Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { if (mView == null) { //防止白页 mView = inflater.inflate(R.layout.fragment_red_packet, container, false); initView(); } return mView; }判断一下是否为null就可以了。
本文针对Tablayout+ViewPager+Fragment结构中Fragment被回收的问题提供了解决方案。当超过四个Fragment时,早期的Fragment会被销毁,文章提供了onCreateView方法中判断mView是否为null的解决方案。
625

被折叠的 条评论
为什么被折叠?



