鍍金池/ 問答/iOS  Android/ viewpager+fragment,從網(wǎng)絡請求數(shù)據(jù),快速滑動后內(nèi)部view方法

viewpager+fragment,從網(wǎng)絡請求數(shù)據(jù),快速滑動后內(nèi)部view方法報空指針

1.情景如題,緩慢滑動等待一個頁面加載設置完成后就不會報錯
代碼遵循mvp結(jié)構(gòu)

public class QuestionAnalyzeFragment extends BaseFragment implements QuestionView{
    @BindView(R.id.tv)
    TextView mTv;
    @BindView(R.id.iv)
    ImageView mIv;
    //.....一些其控件綁定
    private View mRootView;
 @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mRootView == null) {
            mRootView = inflater.inflate(R.layout.fragment_question, container, false);
            ButterKnife.bind(this, mRootView);
            mPresenter = new Presenter(this);
            //獲取網(wǎng)絡數(shù)據(jù)
            mPresenter.getData();
        }
        mUnbinder = ButterKnife.bind(this, mRootView);
        return mRootView;
    }
    //數(shù)據(jù)返回
    @Override
    public void onGetData(Data data){
        mTv.setText(data.getSomeText());
        Glide.with(context)
    .load(data.getUrl())
    .into(mIv);
    }
    
    @Override
     public void onDestroyView() {
        super.onDestroyView();
        mUnbinder.unbind();
    }
}

2.報錯:數(shù)據(jù)返回里使用到的tabLayout,ImageView,在newTab和glide方法都出現(xiàn)了偶現(xiàn)報錯。

3.個人猜測是因為快速滑動,數(shù)據(jù)獲取發(fā)出了,而數(shù)據(jù)返回后,需要set數(shù)據(jù)的控件所在的fragment已經(jīng)onDestroyView()了。
但是沒想好解決方案

回答
編輯回答
尋仙

網(wǎng)絡異步加載更新View可能會導致你說的情況,方案:
1.ViewPager的setOffscreenPageLimit方法提高預加載數(shù)量,保證滑動時不會銷毀fragment,缺點僅限于有限級加載
2.對網(wǎng)絡請求下手,銷毀頁面的同時取消網(wǎng)絡請求
3.網(wǎng)絡請求成功,判斷是否銷毀再行更新View

2018年5月18日 09:30