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

    package com.test.mvp.mvpdemo.mvp.v6.basemvp;

    import android.os.Bundle;
    import android.support.annotation.IdRes;
    import android.support.annotation.LayoutRes;
    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 com.test.mvp.mvpdemo.mvp.v6.inject.InjectPresenter;
    import java.lang.reflect.Field;
    import java.util.ArrayList;
    import java.util.List;

    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() {
    super.onDestroy();
    for (BasePresenter presenter : mInjectPresenters) {
    presenter.detach();
    }
    mInjectPresenters.clear();
    mInjectPresenters = null;
    }
    }
    --------------------- 

  • 相关阅读:
    个人阅读笔记05—数据流图
    JVM(三)程序计数器【PC寄存器】
    JVM(三)运行时数据区的概述,线程概述
    JVM(二)表示两个Class对象是否为同一个类;Java程序对类的使用
    我的面试经之JVM(二)双亲委派机制
    我的面试经之JVM(二) ClassLoader补充【获取类的加载器的方式】
    我的面试经之JVM(二)类加载器的分类
    我的面试经之JVM(二)类加载器子系统ClassLoader类的加载过程
    Python小白要了解的模块大概用途(随学习进度更新),import xxxx
    Python常见的模块(知乎转载)
  • 原文地址:https://www.cnblogs.com/ly570/p/11299121.html
Copyright © 2011-2022 走看看