• 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;
    }
    }
    --------------------- 

  • 相关阅读:
    JavaScript表单编程
    JavaScript事件模型
    DOM总结
    CSS3 媒体查询响应式布局
    BOM总结
    JS实现文字图片无缝滚动
    深入理解jQuery事件绑定机制
    被你忽略的jQuery常用方法“特异功能”
    关于BOM的学习总结
    Hello,World!
  • 原文地址:https://www.cnblogs.com/ly570/p/11299107.html
Copyright © 2020-2023  润新知