FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT 懒加载理解

参考文章1

参考文章2

Fragment懒加载总结Demo地址

  • setMaxLifecycle方法作用是设置Fragment最大生命周期, 比如当前Fragment处于onResume,通过设置setMaxLifecycle(Lifecycle.State.STARTED),可以使Fragment生命周期降级到onStart.

    • 传入Lifecycle.State.STARTED
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    homefragment = new HomeFragment();
    ft.add(R.id.fl_container, homefragment);
    ft.setMaxLifecycle(homefragment, Lifecycle.State.STARTED);
    ft.commit();
    
    • Fragment生命周期方法执行如下,可以看到并不会执行到onResume

    img

  • ViewPager+Fragment 代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mVp = (ViewPager) findViewById(R.id.vp);
    addFragments();
    MyFragmentStatePagerAdapter myFragmentStatePagerAdapter = new MyFragmentStatePagerAdapter(
            getSupportFragmentManager(),
              //FragmentStatePagerAdapter.BEHAVIOR_SET_USER_VISIBLE_HINT, 兼容以前那种方式实现的懒加载
            FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT,
            mFragments);
    mVp.setAdapter(myFragmentStatePagerAdapter);
}
private void addFragments() {
    mFragments = new ArrayList();
    mFragments.add(new FirstFragment());
    mFragments.add(new SecondFragment());
    mFragments.add(new ThirdFragment());
    mFragments.add(new FourthFragment());
}
//适配器
public class MyFragmentStatePagerAdapter extends FragmentStatePagerAdapter {
    ArrayList<Fragment> fragments;

    public MyFragmentStatePagerAdapter(@NonNull FragmentManager fm, int behavior, ArrayList<Fragment> fragments) {
        super(fm, behavior);
        this.fragments = fragments;
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }
}
  • 主要看FragmentStatePagerAdapter中的setPrimaryItem方法,该方法时用来设置哪个Item显示
@Override
@SuppressWarnings({"ReferenceEquality", "deprecation"})
public void setPrimaryItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
    // fragment 为待显示的Fragment,
    // mCurrentPrimaryItem 为当前显示的Fragment
    Fragment fragment = (Fragment)object;
    if (fragment != mCurrentPrimaryItem) {
        if (mCurrentPrimaryItem != null) {
            ...
			// 当传入MyFragmentStatePagerAdapter中的为FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT时,将当前展示的Fragment设置为onStart
            if (mBehavior == BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
                ...
                mCurTransaction.setMaxLifecycle(mCurrentPrimaryItem, Lifecycle.State.STARTED);
            } else {
				// 如果传入的是BEHAVIOR_SET_USER_VISIBLE_HINT,则会执行这行代码,这样可以兼容以前老的懒加载方法.                
                mCurrentPrimaryItem.setUserVisibleHint(false);
            }
        }
        fragment.setMenuVisibility(true);
		// 当传入MyFragmentStatePagerAdapter中的为FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT时,将下面要展示的Fragment设置为onResume        
        if (mBehavior == BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            ...
            mCurTransaction.setMaxLifecycle(fragment, Lifecycle.State.RESUMED);
        } else {
			// 如果传入的是BEHAVIOR_SET_USER_VISIBLE_HINT,则会执行这行代码,这样可以兼容以前老的懒加载方法.              
            fragment.setUserVisibleHint(true);
        }
        mCurrentPrimaryItem = fragment;
    }
}
  • 剩下的在onResume中增加一个标识,就可以实现懒加载了.
public class FirstFragment extends Fragment {
    private static final String TAG = FirstFragment.class.getName();
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        TextView textView = new TextView(container.getContext());
        Log.d(TAG, "onCreateView: 第1个Fragment");
        textView.setText("第一个Fragment");
        ViewGroup.LayoutParams layoutParams = container.getLayoutParams();
        layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        textView.setGravity(Gravity.CENTER);
        textView.setLayoutParams(layoutParams);
        return textView;
    }
    boolean isFirstLoad = true;
    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onResume: 第1个Fragment");
        if (isFirstLoad){
            isFirstLoad = !isFirstLoad;
            Log.d(TAG, "第1个Fragment懒加载");
        }
    }
}

你可能感兴趣的:(FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT 懒加载理解)