自用BaseFragment

public abstract class BaseFragment extends Fragment {

    private View parentView;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        parentView = inflater.inflate(getLayoutId(), container, false);
        return parentView;
    }

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

    //获取布局文件ID
    protected abstract int getLayoutId();

    protected abstract void initView(View view, Bundle savedInstanceState);

    protected abstract void initData();

    protected abstract void onEvent();

    public  T findView(int resId) {
        return (T) (getView().findViewById(resId));
    }

    public BaseActivity getBaseActivity() {
        return (BaseActivity) getActivity();
    }

    public View getParentView() {
        return parentView;
    }
}

你可能感兴趣的:(自用BaseFragment)