【BaseFragment】基于MVP+Dagger2+Retrofit+Rxjava框架之BaseFragment
来源:互联网 发布:持仓异动指标源码 编辑:程序博客网 时间:2024/04/29 18:24
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; }}
阅读全文
0 0
- 【BaseFragment】基于MVP+Dagger2+Retrofit+Rxjava框架之BaseFragment
- 【BaseContract】基于MVP+Dagger2+Retrofit+Rxjava框架之BaseContract
- 【BaseRVFragment】基于MVP+Dagger2+Retrofit+Rxjava框架之BaseRVFragment
- 【IApplication】基于MVP+Dagger2+Retrofit+Rxjava框架之IApplication
- Rxjava + retrofit + dagger2 + mvp搭建Android框架
- MVP+Dagger2+Retrofit+Rxjava
- RxJava+Retrofit+MVP+Dagger2
- RxJava+Retrofit+MVP+Dagger2
- android MVP + dagger2 + Retrofit + Rxjava+okhttp android基础项目框架搭建(1)--之Dagger2引入
- BaseFragment
- BaseFragment
- Android Mvp快速搭建框架MVP+Dagger2+Retrofit+Rxjava
- android MVP + dagger2 + Retrofit + Rxjava+okhttp android基础项目框架搭建(2)--之MVP引入
- mvp框架学习实战代码(配合retrofit+dagger2+rxjava)
- RXjava+Retrofit+dagger2打造自己的MVP框架
- RxJava+Retrofit+MVP框架
- 简单实用的mvp+retrofit+rxjava+dagger2
- Android架构之BaseFragment
- 由浅入深理解java集合(五)——集合-Map
- HDU-1024-DP-(滚动数组优化与状态转移)
- 【几道题】Tarjan练习
- OSGEARTH 数据处理
- 前端组件库合集
- 【BaseFragment】基于MVP+Dagger2+Retrofit+Rxjava框架之BaseFragment
- python 读取文件列表
- 算法谜题72 标记方格2
- C++基础---无返回值函数(void函数)
- 2017年五月二十五日 NO.1 开始使用CSDN--了解MarkDown的语法
- 一些优秀的网站
- Go Timer的使用
- FlyWeight模式
- C++ 拓扑排序