compile
implementation 'com.android.support:support-v4:26.1.0' // compile 'com.tbruyelle.rxpermissions2:rxpermissions:0.9.5@aar' compile 'com.tbruyelle.rxpermissions:rxpermissions:0.9.4@aar' compile 'io.reactivex.rxjava2:rxjava:2.1.8' compile 'com.squareup.retrofit2:converter-gson:2.3.0' compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0' //okhttp log 工具 compile 'com.squareup.okhttp3:logging-interceptor:3.1.2' compile 'com.google.code.gson:gson:2.8.2' compile 'io.reactivex:rxandroid:1.1.0' compile 'com.squareup.retrofit2:retrofit:2.3.0' compile 'com.squareup.retrofit2:converter-scalars:2.3.0' compile 'com.google.dagger:dagger:2.12' annotationProcessor 'com.google.dagger:dagger-compiler:2.9' //適配的終極解決方案 compile 'com.zhy:autolayout:1.4.5' // If you want to bind to Android-specific lifecycles compile 'com.trello.rxlifecycle2:rxlifecycle-android:2.2.1' // If you want pre-written Activities and Fragments you can subclass as providers compile 'com.trello.rxlifecycle2:rxlifecycle-components:2.2.1' implementation 'com.android.support:cardview-v7:26.1.0' implementation 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' compile 'org.greenrobot:eventbus:3.1.1'
BaseActivity
public abstract class BaseActivity extends RxAppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(getLayoutId()); init(savedInstanceState); } protected abstract void init(Bundle savedInstanceState); protected abstract @LayoutRes int getLayoutId(); }
LazyBaseFragment
/** * Created by PVer on 2018/3/24. */ public abstract class LazyBaseFragment extends RxFragment{ protected boolean isFirstUserVisible = true; protected boolean isPrepared = false; private View targetView; private boolean isFirstUserInVisible = true; protected String TAG = "LazyBaseFragment"; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { if (getLayoutId() != 0) { return inflater.inflate(getLayoutId(), container,false); } else { return super.onCreateView(inflater, container, savedInstanceState); } } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); initViewAndEvent(view,savedInstanceState); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); initPrepare(); } @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); Log.v(TAG,"setUserVisibleHint:" + String.valueOf(isVisibleToUser)); if(isVisibleToUser) { //可見 if(isFirstUserVisible) { //第一次用戶可見 isFirstUserVisible = false; initPrepare(); } else { onUserVisible(); } } else { //不可見 if(isFirstUserInVisible) { isFirstUserInVisible = false; onFirstUserInvisible(); } else { onUserInvisible(); } } } private synchronized void initPrepare() { if (isPrepared) { onFirstUserVisible(); } else { isPrepared = true; } } public View getTargetView() { return targetView; } public void setTargetView(View targetView) { this.targetView = targetView; } protected abstract @LayoutRes int getLayoutId(); protected abstract void onFirstUserVisible(); //第一次用戶可見 protected abstract void onUserVisible(); private void onFirstUserInvisible(){} protected abstract void onUserInvisible(); protected abstract void initViewAndEvent(View view,Bundle savedInstanceState); }
對於LazyBaseFragment的說明
假設有四個界面,第一個界面爲Home,第二個爲Exchange,第三個爲Treat,第四個爲Mine。 他們的生命週期調用:java
1. Adapter 繼承的是FragmentStatePagerAdapter :
03-24 23:51:54.981 18649-18649/com.fai.tools V/HomeFragment: setUserVisibleHint:false 03-24 23:51:54.981 18649-18649/com.fai.tools V/ExchangeFragment: setUserVisibleHint:false 03-24 23:51:54.981 18649-18649/com.fai.tools V/HomeFragment: setUserVisibleHint:true 03-24 23:51:54.987 18649-18649/com.fai.tools V/HomeFragment: onCreateView 03-24 23:51:55.001 18649-18649/com.fai.tools V/HomeFragment: onActivityCreated 03-24 23:51:55.001 18649-18649/com.fai.tools V/ExchangeFragment: onCreateView 03-24 23:51:55.018 18649-18649/com.fai.tools V/ExchangeFragment: onActivityCreated
繼續滑動一下ViewPager到ExchangeFragment:react
03-24 23:57:37.090 18649-18649/com.fai.tools V/TreatFragment: setUserVisibleHint:false 03-24 23:57:37.091 18649-18649/com.fai.tools V/HomeFragment: setUserVisibleHint:false 03-24 23:57:37.091 18649-18649/com.fai.tools V/ExchangeFragment: setUserVisibleHint:true 03-24 23:57:37.095 18649-18649/com.fai.tools V/TreatFragment: onCreateView 03-24 23:57:37.132 18649-18649/com.fai.tools V/TreatFragment: onActivityCreated
繼續滑動一下ViewPager到TreatFragment:android
03-25 11:20:59.987 22856-22856/com.fai.tools V/MineFragment: setUserVisibleHint:false 03-25 11:20:59.987 22856-22856/com.fai.tools V/ExchangeFragment: setUserVisibleHint:false 03-25 11:20:59.987 22856-22856/com.fai.tools V/TreatFragment: setUserVisibleHint:true 03-25 11:20:59.989 22856-22856/com.fai.tools V/HomeFragment: onPause 03-25 11:20:59.989 22856-22856/com.fai.tools V/HomeFragment: onStop 03-25 11:20:59.991 22856-22856/com.fai.tools V/HomeFragment: onDestroy 03-25 11:20:59.992 22856-22856/com.fai.tools V/MineFragment: onCreateView 03-25 11:21:00.010 22856-22856/com.fai.tools V/MineFragment: onActivityCreated
繼續滑動一下ViewPager到MineFragment:緩存
03-25 11:22:25.521 22856-22856/com.fai.tools V/TreatFragment: setUserVisibleHint:false 03-25 11:22:25.521 22856-22856/com.fai.tools V/MineFragment: setUserVisibleHint:true 03-25 11:22:25.524 22856-22856/com.fai.tools V/ExchangeFragment: onPause 03-25 11:22:25.524 22856-22856/com.fai.tools V/ExchangeFragment: onStop 03-25 11:22:25.526 22856-22856/com.fai.tools V/ExchangeFragment: onDestroy
2.若增長代碼:ide
viewPager.setOffscreenPageLimit(pagerLimit); //這句話的代碼的做用就是在一開始ViewPager就緩存pagerLimit + 1個頁面, 無論是FragmentPagerStateAdapter仍是FragmentPagerAdapter都是同樣的緩存模式,一開始就緩存,pagerLimit + 1個頁面,log同樣 。 // 雖然FragmentPagerAdapter已經會緩存全部加載過的頁面,他的緩存和viewPager.setOffscreenPageLimit(pagerLimit);區別在於 // 1. FragmentPagerAdapter不是一開始就緩存pagerLimit + 1 ,是加載一個緩存一個,都緩存下來 // 2.FragmentPagerAdapter會將加載過的暫時用不到的fragment進行onPause onStop操做,好比在第三個頁面的時候,會將第一個頁面進行onPause , onStop操做
04-22 17:07:47.110 5114-5114/com.fai.tools V/HomeFragment: setUserVisibleHint:false 04-22 17:07:47.110 5114-5114/com.fai.tools V/ExchangeFragment: setUserVisibleHint:false 04-22 17:07:47.110 5114-5114/com.fai.tools V/TreatFragment: setUserVisibleHint:false 04-22 17:07:47.110 5114-5114/com.fai.tools V/MineFragment: setUserVisibleHint:false 04-22 17:07:47.110 5114-5114/com.fai.tools V/HomeFragment: setUserVisibleHint:true 04-22 17:07:47.114 5114-5114/com.fai.tools V/HomeFragment: onCreateView 04-22 17:07:47.138 5114-5114/com.fai.tools V/HomeFragment: onActivityCreated 04-22 17:07:47.138 5114-5114/com.fai.tools V/ExchangeFragment: onCreateView 04-22 17:07:47.142 5114-5114/com.fai.tools V/ExchangeFragment: onActivityCreated 04-22 17:07:47.142 5114-5114/com.fai.tools V/TreatFragment: onCreateView 04-22 17:07:47.146 5114-5114/com.fai.tools V/TreatFragment: onActivityCreated 04-22 17:07:47.146 5114-5114/com.fai.tools V/MineFragment: onCreateView 04-22 17:07:47.149 5114-5114/com.fai.tools V/MineFragment: onActivityCreated
繼續滑動到下一個頁面 ExchangeFragment工具
04-22 17:12:59.569 5114-5114/com.fai.tools V/HomeFragment: setUserVisibleHint:false 04-22 17:12:59.569 5114-5114/com.fai.tools V/ExchangeFragment: setUserVisibleHint:true
繼續滑動到下一個頁面 TreatFragmentthis
04-22 17:14:27.757 5114-5114/com.fai.tools V/ExchangeFragment: setUserVisibleHint:false 04-22 17:14:27.757 5114-5114/com.fai.tools V/TreatFragment: setUserVisibleHint:true
繼續滑動到下一個頁面MineFragmentgoogle
04-22 17:15:23.909 5114-5114/com.fai.tools V/TreatFragment: setUserVisibleHint:false 04-22 17:15:23.909 5114-5114/com.fai.tools V/MineFragment: setUserVisibleHint:true
3.Adapter 繼承的是FragmentPagerAdapter :
03-25 11:07:57.600 18382-18382/com.fai.tools V/HomeFragment: setUserVisibleHint:false 03-25 11:07:57.600 18382-18382/com.fai.tools V/ExchangeFragment: setUserVisibleHint:false 03-25 11:07:57.600 18382-18382/com.fai.tools V/HomeFragment: setUserVisibleHint:true 03-25 11:07:57.604 18382-18382/com.fai.tools V/HomeFragment: onCreateView 03-25 11:07:57.609 18382-18382/com.fai.tools V/HomeFragment: onActivityCreated 03-25 11:07:57.610 18382-18382/com.fai.tools V/ExchangeFragment: onCreateView 03-25 11:07:57.614 18382-18382/com.fai.tools V/ExchangeFragment: onActivityCreated
繼續滑動到下一個頁面ExchangeFragment:scala
03-25 11:08:32.784 18382-18382/com.fai.tools V/TreatFragment: setUserVisibleHint:false 03-25 11:08:32.784 18382-18382/com.fai.tools V/HomeFragment: setUserVisibleHint:false 03-25 11:08:32.784 18382-18382/com.fai.tools V/ExchangeFragment: setUserVisibleHint:true 03-25 11:08:32.787 18382-18382/com.fai.tools V/TreatFragment: onCreateView 03-25 11:08:32.805 18382-18382/com.fai.tools V/TreatFragment: onActivityCreated
繼續滑動一下ViewPager到TreatFragment:code
03-25 11:08:52.785 18382-18382/com.fai.tools V/MineFragment: setUserVisibleHint:false 03-25 11:08:52.785 18382-18382/com.fai.tools V/ExchangeFragment: setUserVisibleHint:false 03-25 11:08:52.785 18382-18382/com.fai.tools V/TreatFragment: setUserVisibleHint:true 03-25 11:08:52.788 18382-18382/com.fai.tools V/HomeFragment: onPause 03-25 11:08:52.788 18382-18382/com.fai.tools V/HomeFragment: onStop 03-25 11:08:52.791 18382-18382/com.fai.tools V/MineFragment: onCreateView 03-25 11:08:52.806 18382-18382/com.fai.tools V/MineFragment: onActivityCreated
繼續滑動一下ViewPager到MineFragment:
03-25 11:09:13.006 18382-18382/com.fai.tools V/TreatFragment: setUserVisibleHint:false 03-25 11:09:13.006 18382-18382/com.fai.tools V/MineFragment: setUserVisibleHint:true 03-25 11:09:13.008 18382-18382/com.fai.tools V/ExchangeFragment: onPause 03-25 11:09:13.008 18382-18382/com.fai.tools V/ExchangeFragment: onStop
FragmentStatePagerAdapter和FragmentPagerAdapter的區別:
FragmentStatePagerAdapter不會保存全部Pager的狀態,最多保存上一個,本身,下一個。其餘的都會執行onDestroy方法
FragmentPagerAdapter不會執行onDestroy方法,會保存全部的狀態。