您的位置:首页 > 移动开发 > Android开发

策略模式 - Android.TabHost

2016-07-29 18:44 435 查看
android.widget.TabHost

设计模式在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
这两个接口。不得不说这样的设计很有意思,使两个策略完全对客户端透明,只是客户端通过调用不同的方法就可以使用不同的策略,很值得学习
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息