Android学习笔记--BaseActivity和BaseFragment的再抽取
来源:互联网 发布:淘宝比价格的软件 编辑:程序博客网 时间:2024/05/17 22:15
前言
以前都是对Activity或Fragment进行抽取,封装成BaseActivity或BaseFragment基类。随着Activity或Fragment的增加,往往基类的抽象方法就会越来越来多,但是很多时候它们的抽象方法是一样的。那么其实可以进一步的抽取,封装到一个接口当中。
抽取基类思想
- 父类可以直接加载子类的共同布局
- 父类需要定义子类所有的共同方法
- 父类需要实现子类方法的共同调用逻辑
- 子类必须实现的方法,那么父类应该抽象出来
- 子类有可能重写的方法,那么父类不能写死,应该抽出方法进行默认实现,允许子类重写
BaseActivity
以前的是这样的
public abstract class BaseActivity extends FragmentActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(getLayoutId()); ButterKnife.bind(this); initView(); initData(); initListener(); } protected abstract void initListener(); protected abstract void initData(); protected abstract void initView(); protected abstract int getLayoutId();}
当然了实际代码可能比上面更加复杂。
BaseFragment
以前的是这样的
public abstract class BaseFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view =View.inflate(getActivity(),getLayoutId(),null); ButterKnife.bind(this,view); initView(); initData(); initListener(); return view; } protected abstract void initListener(); protected abstract void initData(); protected abstract void initView(); protected abstract int getLayoutId();}
更好的改进
BaseActivity
public abstract class BaseActivity extends FragmentActivity implements UiOperation{ @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(getLayoutId()); ButterKnife.bind(this); initData(); initListener(); }}
BaseFragment
public abstract class BaseFragment extends Fragment implements UiOperation{ @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view =View.inflate(getActivity(),getLayoutId(),null); ButterKnife.bind(this,view); initListener(); initData(); return view; }}
UiOperation
public interface UiOperation { /**绑定布局Id**/ int getLayoutId(); /**绑定接口*/ void initListener(); /**绑定数据**/ void initData();}
通过抽取UiOperation,抽取BaseActivity和BaseFragment的公共的方法。
0 0
- Android学习笔记--BaseActivity和BaseFragment的再抽取
- android 之 Baseactivity 和BaseFragment
- BaseActivity和BaseFragment 的封装
- android BaseActivity与BaseFragment的封装
- BaseActivity和BaseFragment封装
- 构建android项目的两个重要的基类BaseFragment和BaseActivity
- android 抽取BaseFragment
- Android谈谈封装那些事--BaseActivity和BaseFragment(一)
- Android谈谈封装那些事--BaseActivity和BaseFragment(二)
- 项目优化-BaseActivity和BaseFragment
- BaseActivity与BaseFragment的封装
- BaseActivity与BaseFragment的封装
- BaseFragment的抽取
- BaseFragment的一般抽取
- Android BaseActivity、BaseFragment等 常用方法封装
- 从BaseActivity与BaseFragment的封装谈起
- 从BaseActivity与BaseFragment的封装谈起
- 从BaseActivity与BaseFragment的封装谈起
- 索引排序文件--不排序数据排序索引暨介绍大数据常用的显示数据的两种方式
- 简述什么是框架
- 如何转载博客
- Qt 点线效果
- Android开发——RecyclerView特性以及基本使用方法(二)
- Android学习笔记--BaseActivity和BaseFragment的再抽取
- 处子之身写博客
- pycharm上安装使用easygui
- android学习的一些看法
- Codeforces Round #389 Technocup 2017 E. Santa Claus and Tangerines(二分+DP)
- 怎么设置jsp的text文本框的内容不能修改
- Swift-继承、重写
- 我的框架--QiCai(二)
- HttpClient 4.5版本设置连接超时时间