【BaseFragment】基于MVP+Dagger2+Retrofit+Rxjava框架之BaseFragment

base类是基础,小伙伴先大致看一下思路后面等整个框架带领大家搭建起来以后我再给出项目的整个地址

返回父布局parentView

protected View parentView;
//onCreateView()里
parentView = inflater.inflate(getLayoutResId(), container, false);
return parentView;

//涉及getLayoutResId(),必须重写 方法
//创建方法获取
protected View getParentView() {
        return parentView;
    }

获取FragmentActivity对象

protected FragmentActivity activity;
//onCreate()里
activity = getSupportActivity();

获取LayoutInflater对象

protected LayoutInflater inflater;
//onCreateView里
this.inflater = inflater;
//获取
protected LayoutInflater getLayoutInflater() {
        return inflater;
    }

获取Context对象

protected Context mContext;
//onCreate()里
//前面获取了FragmentActivity对象
mContext = activity;

设置重写必要的方法

//onViewCreated里
setupActivityComponent(ReaderApplication.getsInstance().getAppComponent());
attachView();
initDatas();
configViews();

//方法
 //必须重写的方法
    public abstract
    @LayoutRes
    int getLayoutResId();
    //设置dagger2桥梁
    protected abstract void setActivityCompontent(AppComponent appComponent);
    public abstract void attachView();
    public abstract void initDatas();
    public abstract void configViews();//对控件进行设置适配,填充数据

生命周期

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnbinder.unbind();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = (FragmentActivity) activity;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        this.activity = null;
    }

获取一些数据的方法

public Context getApplicationContext() {
        return this.activity == null ? (getActivity() == null ? null : getActivity()
                .getApplicationContext()) : this.activity.getApplicationContext();
    }
    protected LayoutInflater getLayoutInflater() {
        return inflater;
    }
    protected View getParentView() {
        return parentView;
    }
    public FragmentActivity getSupportActivity() {
        return super.getActivity();
    }

给个实例

package com.daqsoft.baseproject.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.daqsoft.baseproject.component.AppComponent;
import com.daqsoft.baseproject.view.loadding.CustomDialog;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/** * Created by yanbo on 2017/5/17. */
public abstract class BaseFragment extends Fragment {
    protected View parentView;
    protected FragmentActivity activity;
    protected LayoutInflater inflater;
    protected Context mContext;
    private CustomDialog dialog;

    private Unbinder mUnbinder;//黄油刀

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) {
        parentView = inflater.inflate(getLayoutResId(), container, false);
        activity = getSupportActivity();
        mContext = activity;
        this.inflater = inflater;
        return parentView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mUnbinder = ButterKnife.bind(this,view);
        setActivityCompontent(IApplication.getInstance().getAppComponent());
        attachView();
        initDatas();
        configViews();

    }


    //必须重写的方法
    public abstract
    @LayoutRes
    int getLayoutResId();
    //设置dagger2桥梁
    protected abstract void setActivityCompontent(AppComponent appComponent);
    public abstract void attachView();
    public abstract void initDatas();
    public abstract void configViews();//对控件进行设置适配,填充数据


    /** * 生命周期 */

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnbinder.unbind();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = (FragmentActivity) activity;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        this.activity = null;
    }

    /** *获取一些数据的方法 * */
    public Context getApplicationContext() {
        return this.activity == null ? (getActivity() == null ? null : getActivity()
                .getApplicationContext()) : this.activity.getApplicationContext();
    }
    protected LayoutInflater getLayoutInflater() {
        return inflater;
    }
    protected View getParentView() {
        return parentView;
    }
    public FragmentActivity getSupportActivity() {
        return super.getActivity();
    }


    /** * 加载进度框 */
    public CustomDialog getDialog() {
        if (dialog == null) {
            dialog = CustomDialog.instance(getActivity());
            dialog.setCancelable(false);
        }
        return dialog;
    }
    public void hideDialog() {
        if (dialog != null)
            dialog.hide();
    }
    public void showDialog() {
        getDialog().show();
    }
    public void dismissDialog() {
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
    }

    /** * view的隐藏 */
    protected void gone(final View... views) {
        if (views != null && views.length > 0) {
            for (View view : views) {
                if (view != null) {
                    view.setVisibility(View.GONE);
                }
            }
        }
    }
    protected void visible(final View... views) {
        if (views != null && views.length > 0) {
            for (View view : views) {
                if (view != null) {
                    view.setVisibility(View.VISIBLE);
                }
            }
        }

    }
    protected boolean isVisible(View view) {
        return view.getVisibility() == View.VISIBLE;
    }

}

你可能感兴趣的:(框架,mvp)