策略模式 - Android.TabHost
2016-07-29 18:44
435 查看
android.widget.TabHost
设计模式在Android源码中的使用。
观察
接口的名字名字起得简单明了,分别是指示器的策略和内容的策略。
定义在TabHost当中的两个策略接口
以及部分
可以看到,TabSpec对外隐藏了
设计模式在Android源码中的使用。
观察
android.widget.TabHost中的内部类
TabSpec的源码后发现,其内部使用了策略模式,而且使用的十分巧妙。
接口的名字名字起得简单明了,分别是指示器的策略和内容的策略。
public class TabSpec { private IndicatorStrategy mIndicatorStrategy; private ContentStrategy mContentStrategy; /** * Specify a label as the tab indicator. */ public TabSpec setIndicator(CharSequence label) { mIndicatorStrategy = new LabelIndicatorStrategy(label); return this; } /** * Specify a label and icon as the tab indicator. */ public TabSpec setIndicator(CharSequence label, Drawable icon) { mIndicatorStrategy = new LabelAndIconIndicatorStrategy(label, icon); return this; } /** * Specify a view as the tab indicator. */ public TabSpec setIndicator(View view) { mIndicatorStrategy = new ViewIndicatorStrategy(view); return this; } /** * Specify the id of the view that should be used as the content * of the tab. */ public TabSpec setContent(int viewId) { mContentStrategy = new ViewIdContentStrategy(viewId); return this; } /** * Specify a {@link android.widget.TabHost.TabContentFactory} to use to * create the content of the tab. */ public TabSpec setContent(TabContentFactory contentFactory) { mContentStrategy = new FactoryContentStrategy(mTag, contentFactory); return this; } /** * Specify an intent to use to launch an activity as the tab content. */ public TabSpec setContent(Intent intent) { mContentStrategy = new IntentContentStrategy(mTag, intent); return this; } }
定义在TabHost当中的两个策略接口
IndicatorStrategy和
ContentStrategy
/** * Specifies what you do to create a tab indicator. */ private static interface IndicatorStrategy { /** * Return the view for the indicator. */ View createIndicatorView(); }
private static interface ContentStrategy { /** * Return the content view. The view should may be cached locally. */ View getContentView(); /** * Perhaps do something when the tab associated with this content has * been closed (i.e make it invisible, or remove it). */ void tabClosed(); }
以及部分
IndicatorStrategy的实现
/** * How to create a tab indicator that just has a label. */ private class LabelIndicatorStrategy implements IndicatorStrategy { private final CharSequence mLabel; private LabelIndicatorStrategy(CharSequence label) { mLabel = label; } public View createIndicatorView() { final Context context = getContext(); LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View tabIndicator = inflater.inflate(mTabLayoutId, mTabWidget, // tab widget is the parent false); // no inflate params final TextView tv = (TextView) tabIndicator.findViewById(R.id.title); tv.setText(mLabel); if (context.getApplicationInfo().targetSdkVersion <= Build.VERSION_CODES.DONUT) { // Donut apps get old color scheme tabIndicator.setBackgroundResource(R.drawable.tab_indicator_v4); tv.setTextColor(context.getColorStateList(R.color.tab_indicator_text_v4)); } return tabIndicator; } } /** * How to create a tab indicator by specifying a view. */ private class ViewIndicatorStrategy implements IndicatorStrategy { private final View mView; private ViewIndicatorStrategy(View view) { mView = view; } public View createIndicatorView() { return mView; } }
可以看到,TabSpec对外隐藏了
IndicatorStrategy和
ContentStrategy这两个接口。不得不说这样的设计很有意思,使两个策略完全对客户端透明,只是客户端通过调用不同的方法就可以使用不同的策略,很值得学习
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories