所有Fragment的基类
/**
* 所有Fragment的基类
*/
public abstract class BaseFragment_Ma extends Fragment {
private View rootView;
private Unbinder unBinder;
protected Context mContext;
protected LoadingDialog loadingDialog;
protected Activity mActivity;
@Override
public void onAttach(Context context) {
super.onAttach(context);
mActivity = (Activity) context;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = getActivity();
loadingDialog = new LoadingDialog(mContext);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_base, container, false);
((ViewGroup) rootView.findViewById(R.id.fl_content)).addView(getLayoutInflater().inflate(getLayoutId(), null));
unBinder = ButterKnife.bind(this, rootView);
if (regEvent()) {
EventBus.getDefault().register(this);
}
return rootView;
}
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initView();
}
@Override
public void onDestroy() {
super.onDestroy();
if (unBinder != null) {
unBinder.unbind();
}
if (regEvent()) {
EventBus.getDefault().unregister(this);
}
}
/**
* 子类接受事件 重写该方法
*/
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventBus(EventBus_Message event) {
}
/**
* 需要接收事件 重写该方法 并返回true
*/
protected boolean regEvent() {
return false;
}
protected abstract int getLayoutId();
protected abstract void initView();
protected String getStringText(int result_ID) {
return mContext.getResources().getString(result_ID);
}
}