考虑两层嵌套,很全面的懒加载Fragment

package com.example.lazyloadvp.Lazy2;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

public abstract class LazyFragment extends Fragment {

    private View rootView = null;
    // view是否被创建
    private boolean mIsViewCreated = false;
    //是否第一次创建
    private boolean mIsFirstVisible = false;

    //表示Fragment 不可见状态和再次回到可见状态
    private boolean mCurrentVisibleState = false;

    public abstract int getLayoutRes();
    public abstract void initView(View view);

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(rootView == null){
            rootView = inflater.inflate(getLayoutRes(),null);
        }
        initView(rootView);

        mIsViewCreated = true;

        //没有隐藏 并且可见  分发状态true
        if(!isHidden() && getUserVisibleHint()){
            dispatchUserVisibleHint(true);
        }

        return rootView;
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(mIsViewCreated){
            if(isVisibleToUser && !mCurrentVisibleState){
                dispatchUserVisibleHint(true);
            }else if(!isVisibleToUser && mCurrentVisibleState){
                dispatchUserVisibleHint(false);
            }
        }

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);

        if(hidden){
            dispatchUserVisibleHint(false);
        }else {
            dispatchUserVisibleHint(true);
        }

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    private void dispatchUserVisibleHint(boolean isVisible){
        if(isVisible && isParentInvisible()){
            return;
        }

        if(mCurrentVisibleState == isVisible){
            return;
        }
        mCurrentVisibleState = isVisible;
        if(isVisible){
            if(mIsFirstVisible){
                mIsFirstVisible = false;
                onFragmentFirstVisible();
            }
            onFragmentResume();
            dispatchChildUserVisibleHint(true);
        }else {
            onFragmentPause();
            dispatchChildUserVisibleHint(false);
        }
    }

    public boolean isParentInvisible(){
        Fragment parentFragment = getParentFragment();
        if(parentFragment instanceof LazyFragment){
            LazyFragment lazyFragment = (LazyFragment) parentFragment;
            return !lazyFragment.mCurrentVisibleState;
        }
        return false;
    }

    @Override
    public void onResume() {
        super.onResume();
        if(!mIsFirstVisible){
            if(!isHidden() && !mCurrentVisibleState && getUserVisibleHint()){
                dispatchUserVisibleHint(true);
            }
        }

    }

    public void dispatchChildUserVisibleHint(boolean isVisible){
        List fragments = getChildFragmentManager().getFragments();
        if(fragments != null){
            for(Fragment fragment : fragments){
                if(fragment instanceof LazyFragment && !fragment.isHidden() && fragment.getUserVisibleHint()){
                    dispatchUserVisibleHint(isVisible);
                }
            }
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if(mCurrentVisibleState && getUserVisibleHint()){
            dispatchUserVisibleHint(false);
        }
    }

    public  void onFragmentFirstVisible(){

    }
    public abstract void onFragmentResume();
    public void onFragmentPause(){
    }

}

你可能感兴趣的:(考虑两层嵌套,很全面的懒加载Fragment)