简单Base封装

 //沉浸式
    implementation 'com.gyf.immersionbar:immersionbar:2.3.3'
    
public abstract class BaseActivity extends AppCompatActivity implements IContract.IView {

    private Unbinder bind;
    private NetWorkBroad netWorkBroad;
    private PresenterImpl presenter;

    public abstract int initLayout();
    public abstract void initView();
    public abstract void initData();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(initLayout());
        bind = ButterKnife.bind(this);
        ImmersionBar.with(this).init();
        setNetWork();
        //p
        presenter = new PresenterImpl();
        presenter.onAttch(this);

        initView();
        initData();
    }

    private void setNetWork() {
        netWorkBroad = new NetWorkBroad();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(netWorkBroad, intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bind != null){
            bind.unbind();
        }
        if (presenter != null){
            presenter.onDeatAttch();
        }
        unregisterReceiver(netWorkBroad);
    }
}
public abstract class BaseFragment extends Fragment implements IContract.IView {
    //判断页面是否可见
    protected boolean isVisibleToUser;
    //判断页面是否加载过
    protected boolean isViewInitiated;
    //判断数据是否加载过
    protected boolean isDataInitiated;
    private View mView;

    public PresenterImpl presenter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = init(inflater);
        return mView;
    }
    protected abstract int getLayoutById();
    protected abstract void initView(View view);
    private View init(LayoutInflater inflater) {
        mView = inflater.inflate(getLayoutById(), null);
        initView(mView);
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isViewInitiated = true;
        //处理p层
        presenter = new PresenterImpl();
        presenter.onAttch(this);
        loadData();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        loadData();
    }

    private void loadData() {
        if (isVisibleToUser && isViewInitiated){
            lazy();
        }
    }

    protected abstract void lazy();

    @Override
    public void onDetach() {
        super.onDetach();
        if (presenter != null){
            presenter.onDeatAttch();
        }
    }
}

3

public class NetWorkBroad extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())){
            NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            if (NetworkInfo.State.CONNECTED == info.getState()){
                Toast.makeText(context, "有网络", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(context, "无网络", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
public abstract class BasePresenter implements IContract.IPresenter {
    private IContract.IView iView;
    private IContract.IModel iModel;

    public abstract void getMethod(String url, Map map, Class mClass);
    public abstract void getMethodB(String url,Map map,Class mClass);
    @Override
    public void onAttch(IContract.IView iView) {
        this.iView = iView;
        iModel = new ModelImpl();
    }

    @Override
    public void startGetInfo(String url, Map map, final Class mClass) {
        iModel.getInfo(url, map, new MyBaseUtils.MyCallBack() {
            @Override
            public void onSuccess(String jsonStr) {
                Gson gson = new Gson();
                Object o = gson.fromJson(jsonStr, mClass);
                iView.getData(o);
            }
        });
    }

    @Override
    public void onDeatAttch() {
        iView = null;
        iModel = null;
    }
}
public class PresenterImpl extends BasePresenter {
    @Override
    public void getMethod(String url, Map map, Class mClass) {
        startGetInfo(url, map, mClass);
    }

    @Override
    public void getMethodB(String url, Map map, Class mClass) {
        startGetInfo(url, map, mClass);
    }
    /*@Override
    protected void getMethod(String url, Map map, Class mClass) {
        startGetInfo(url, map, mClass);
    }*/
}

你可能感兴趣的:(简单Base封装)