zoukankan      html  css  js  c++  java
  • BaseFragment 基类代码


    public abstract class BaseFragment extends Fragment implements IBaseView {

    private List<BasePresenter> mInjectPresenters;

    private View mLayoutView;

    protected abstract @LayoutRes int setLayout();

    protected abstract void initViews(@Nullable Bundle savedInstanceState);

    protected abstract void initData();

    @SuppressWarnings("ConstantConditions")
    protected <T extends View> T $(@IdRes int viewId) {
    return this.getView().findViewById(viewId);
    }

    @SuppressWarnings({"unchecked", "TryWithIdenticalCatches"})
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(setLayout(), container, false);

    mInjectPresenters = new ArrayList<>();

    //获得已经申明的变量,包括私有的
    Field[] fields = this.getClass().getDeclaredFields();
    for (Field field : fields) {
    //获取变量上面的注解类型
    InjectPresenter injectPresenter = field.getAnnotation(InjectPresenter.class);
    if (injectPresenter != null) {
    try {
    Class<? extends BasePresenter> type = (Class<? extends BasePresenter>) field.getType();
    BasePresenter mInjectPresenter = type.newInstance();
    //绑定
    mInjectPresenter.attach(this);
    field.setAccessible(true);
    field.set(this, mInjectPresenter);
    mInjectPresenters.add(mInjectPresenter);
    } catch (IllegalAccessException e) {
    e.printStackTrace();
    } catch (java.lang.InstantiationException e) {
    e.printStackTrace();
    } catch (ClassCastException e) {
    e.printStackTrace();
    throw new RuntimeException("SubClass must extends Class:BasePresenter");
    }
    }
    }
    return view;
    }

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

    initViews(savedInstanceState);
    initData();
    }

    @Override
    public void onDestroy(http://www.my516.com) {
    super.onDestroy();
    for (BasePresenter presenter : mInjectPresenters) {
    presenter.detach();
    }
    mInjectPresenters.clear();
    mInjectPresenters = null;
    }
    }
    --------------------- 

  • 相关阅读:
    EXCEL文本转数值方法我找的好苦啊(转) Kevin
    C# 自定义控件和自定义事件 Kevin
    MVC3 示例项目中 Authentication 验证密码错误。 Kevin
    如何选定文件或文件夹
    软件工程师的必修课:PKM
    “个人知识管理”的定义和包含的内容
    如何评价一个专业性的工具软件
    教你从“搜索”的角度来选取个人知识管理软件
    国内领先的PKM(个人知识库管理)工具
    如果界面还不行就跳闽江
  • 原文地址:https://www.cnblogs.com/ly570/p/11299107.html
Copyright © 2011-2022 走看看