上次寫了一個問答項目,用的fragment+viewpager架構,後來發現,劃了幾次之後,再劃回來,會重新加載布局,重新獲取數據,這樣整個程序和卡,並且占用太多的網絡資源。
當時的解決辦法是,自己重寫view,用最基本的Basepageradapder,算是不是很優雅的解決了問題。
當時知道是重新調用onCreateView方法原因,但是沒有好的解決辦法,現在知道了,這裡記載一下。
我實驗了viewpager加載四個fragment:
第一次進入的時候:
03-01 13:50:16.151 22667-22667/com.graypn.modelproject I/onCreate1﹕ onCreate 03-01 13:50:16.151 22667-22667/com.graypn.modelproject I/onCreateView1﹕ onCreateView 03-01 13:50:16.161 22667-22667/com.graypn.modelproject I/onActivityCreated1﹕ onActivityCreated 03-01 13:50:16.161 22667-22667/com.graypn.modelproject I/onCreate2﹕ onCreate 03-01 13:50:16.161 22667-22667/com.graypn.modelproject I/onCreateView2﹕ onCreateView 03-01 13:50:16.161 22667-22667/com.graypn.modelproject I/onActivityCreated2﹕ onActivityCreated往右滑動到第二個界面:
03-01 13:51:22.391 22667-22667/com.graypn.modelproject I/onCreate3﹕ onCreate 03-01 13:51:22.391 22667-22667/com.graypn.modelproject I/onCreateView3﹕ onCreateView 03-01 13:51:22.401 22667-22667/com.graypn.modelproject I/onActivityCreated3﹕ onActivityCreated往右滑動到第三個界面:
03-01 13:55:24.351 24165-24165/com.graypn.modelproject I/onDestroyView1﹕ onDestroyView 03-01 13:55:24.351 24165-24165/com.graypn.modelproject I/onCreate4﹕ onCreate 03-01 13:55:24.351 24165-24165/com.graypn.modelproject I/onCreateView4﹕ onCreateView 03-01 13:55:24.361 24165-24165/com.graypn.modelproject I/onActivityCreated4﹕ onActivityCreated往右滑動到第四個界面:
03-01 13:56:22.021 24165-24165/com.graypn.modelproject I/onDestroyView2﹕ onDestroyView往左劃到第三個界面:
03-01 13:58:09.541 24165-24165/com.graypn.modelproject I/onCreateView2﹕ onCreateView 03-01 13:58:09.541 24165-24165/com.graypn.modelproject I/onActivityCreated2﹕ onActivityCreated
結論:viewpager會加載和當前頁面相連的兩個fragment,會銷毀相鄰第三個頁面的view,再次調用是會重新oncreateview和onactivityreate。
我們需要優化自己的fragment,如下時我優化後的類:
/** * 提供了fragment的封裝後基類,提供context給子類使用 * * @author Graypn */ public abstract class BaseFragment extends Fragment { //根部view private View rootView; protected Context context; private Boolean hasInitData = false; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); context = getActivity(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (rootView == null) { rootView = initView(inflater); } return rootView; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if (!hasInitData) { initData(); hasInitData = true; } } @Override public void onDestroyView() { super.onDestroyView(); ((ViewGroup) rootView.getParent()).removeView(rootView); } /** * 子類實現初始化View操作 */ protected abstract View initView(LayoutInflater inflater); /** * 子類實現初始化數據操作(子類自己調用) */ public abstract void initData(); /** * 封裝從網絡下載數據 */ protected void loadData(HttpRequest.HttpMethod method, String url, RequestParams params, RequestCallBackcallback) { if (0 == NetUtils.isNetworkAvailable(getActivity())) { new CustomToast(getActivity(), 無網絡,請檢查網絡連接!, 0).show(); } else { NetUtils.loadData(method, url, params, callback); } } }
加入判斷是否已經加載數據完成的標志變量,如果已經加載了數據,就不重新加載數據。
算是優雅的解決了優化的問題,希望能幫到有同樣問題的同學。