本文主要是介绍【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@LayoutResint getLayoutResId();//设置dagger2桥梁protected abstract void setActivityCompontent(AppComponent appComponent);public abstract void attachView();public abstract void initDatas();public abstract void configViews();//对控件进行设置适配,填充数据
生命周期
@Overridepublic void onDestroyView() {super.onDestroyView();mUnbinder.unbind();}@Overridepublic void onAttach(Activity activity) {super.onAttach(activity);this.activity = (FragmentActivity) activity;}@Overridepublic 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;//黄油刀@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) {parentView = inflater.inflate(getLayoutResId(), container, false);activity = getSupportActivity();mContext = activity;this.inflater = inflater;return parentView;}@Overridepublic 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@LayoutResint getLayoutResId();//设置dagger2桥梁protected abstract void setActivityCompontent(AppComponent appComponent);public abstract void attachView();public abstract void initDatas();public abstract void configViews();//对控件进行设置适配,填充数据/*** 生命周期*/@Overridepublic void onDestroyView() {super.onDestroyView();mUnbinder.unbind();}@Overridepublic void onAttach(Activity activity) {super.onAttach(activity);this.activity = (FragmentActivity) activity;}@Overridepublic 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;}}
这篇关于【BaseFragment】基于MVP+Dagger2+Retrofit+Rxjava框架之BaseFragment的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!