当前位置: 移动技术网 > IT编程>移动开发>Android > Android BaseActivity与BaseFragmnt的封装方法

Android BaseActivity与BaseFragmnt的封装方法

2018年09月12日  | 移动技术网IT编程  | 我要评论

交通问题,3u8730,郑国淋

注意了
需要使用butterknife进行初始化

baseactivity:

public abstract class activity extends appcompatactivity {

    protected placeholderview mplaceholderview;

    @override
    protected void oncreate(@nullable bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        // 在界面未初始化之前调用的初始化窗口
        initwidows();

        if (initargs(getintent().getextras())) {
            // 得到界面id并设置到activity界面中
            int layid = getcontentlayoutid();
            setcontentview(layid);
            initbefore();
            initwidget();
            initdata();
        } else {
            finish();
        }
    }

    /**
     * 初始化控件调用之前
     */
    protected void initbefore() {

    }

    /**
     * 初始化窗口
     */
    protected void initwidows() {

    }

    /**
     * 初始化相关参数
     *
     * @param bundle 参数bundle
     * @return 如果参数正确返回true,错误返回false
     */
    protected boolean initargs(bundle bundle) {
        return true;
    }

    /**
     * 得到当前界面的资源文件id
     *
     * @return 资源文件id
     */
    protected abstract int getcontentlayoutid();

    /**
     * 初始化控件
     */
    protected void initwidget() {
        butterknife.bind(this);
    }

    /**
     * 初始化数据
     */
    protected void initdata() {

    }


    @override
    public boolean onsupportnavigateup() {
        // 当点击界面导航返回时,finish当前界面
        finish();
        return super.onsupportnavigateup();
    }

    @override
    public void onbackpressed() {
        // 得到当前activity下的所有fragment
        @suppresslint("restrictedapi")
        list fragments = getsupportfragmentmanager().getfragments();
        // 判断是否为空
        if (fragments != null && fragments.size() > 0) {
            for (fragment fragment : fragments) {
                // 判断是否为我们能够处理的fragment类型
                if (fragment instanceof net.qiujuer.italker.common.app.fragment) {
                    // 判断是否拦截了返回按钮
                    if (((net.qiujuer.italker.common.app.fragment) fragment).onbackpressed()) {
                        // 如果有直接return
                        return;
                    }
                }
            }
        }

        super.onbackpressed();
        finish();
    }

    /**
     * 设置占位布局
     *
     * @param placeholderview 继承了占位布局规范的view
     */
    public void setplaceholderview(placeholderview placeholderview) {
        this.mplaceholderview = placeholderview;
    }
}

basefragment:

public abstract class fragment extends android.support.v4.app.fragment {
    protected view mroot;
    protected unbinder mrootunbinder;
    protected placeholderview mplaceholderview;
    // 标示是否第一次初始化数据
    protected boolean misfirstinitdata = true;

    @override
    public void onattach(context context) {
        super.onattach(context);

        // 初始化参数
        initargs(getarguments());

    }

    @nullable
    @override
    public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) {
        if (mroot == null) {
            int layid = getcontentlayoutid();
            // 初始化当前的跟布局,但是不在创建时就添加到container里边
            view root = inflater.inflate(layid, container, false);
            initwidget(root);
            mroot = root;
        } else {
            if (mroot.getparent() != null) {
                // 把当前root从其父控件中移除
                ((viewgroup) mroot.getparent()).removeview(mroot);
            }
        }

        return mroot;
    }

    @override
    public void onviewcreated(view view, @nullable bundle savedinstancestate) {
        super.onviewcreated(view, savedinstancestate);
        if (misfirstinitdata) {
            // 触发一次以后就不会触发
            misfirstinitdata = false;
            // 触发
            onfirstinit();
        }

        // 当view创建完成后初始化数据
        initdata();
    }

    /**
     * 初始化相关参数
     */
    protected void initargs(bundle bundle) {

    }

    /**
     * 得到当前界面的资源文件id
     *
     * @return 资源文件id
     */
    @layoutres
    protected abstract int getcontentlayoutid();

    /**
     * 初始化控件
     */
    protected void initwidget(view root) {
        mrootunbinder = butterknife.bind(this, root);
    }

    /**
     * 初始化数据
     */
    protected void initdata() {

    }

    /**
     * 当首次初始化数据的时候会调用的方法
     */
    protected void onfirstinit() {

    }

    /**
     * 返回按键触发时调用
     *
     * @return 返回true代表我已处理返回逻辑,activity不用自己finish。
     * 返回false代表我没有处理逻辑,activity自己走自己的逻辑
     */
    public boolean onbackpressed() {
        return false;
    }


    /**
     * 设置占位布局
     *
     * @param placeholderview 继承了占位布局规范的view
     */
    public void setplaceholderview(placeholderview placeholderview) {
        this.mplaceholderview = placeholderview;
    }

}

placeholderview:

/**
 * 基础的占位布局接口定义
 *
 */
public interface placeholderview {

    /**
     * 没有数据
     * 显示空布局,隐藏当前数据布局
     */
    void triggerempty();

    /**
     * 网络错误
     * 显示一个网络错误的图标
     */
    void triggerneterror();

    /**
     * 加载错误,并显示错误信息
     *
     * @param strres 错误信息
     */
    void triggererror(@stringres int strres);

    /**
     * 显示正在加载的状态
     */
    void triggerloading();

    /**
     * 数据加载成功,
     * 调用该方法时应该隐藏当前占位布局
     */
    void triggerok();

    /**
     * 该方法如果传入的isok为true则为成功状态,
     * 此时隐藏布局,反之显示空数据布局
     *
     * @param isok 是否加载成功数据
     */
    void triggerokorempty(boolean isok);
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网