以下是封装的库源码:
package com.example.oldtab; import java.util.ArrayList; import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView; public class NsFragmentTabHost implements TabHost.OnTabChangeListener {
private FragmentTabHost fragmentTabHost;
private LayoutInflater layoutInflater;
private Resources resources;
private ArrayList<NsFragmentItem> tabs;
private NsFragmentItem preItem; NsFragmentTabHost(FragmentTabHost tabhost, ActionBarActivity activity) {
this(tabhost, activity, android.R.id.tabcontent);
} NsFragmentTabHost(FragmentTabHost tabhost, ActionBarActivity activity,
int realContentid) {
fragmentTabHost = tabhost;
tabs = new ArrayList<NsFragmentItem>();
layoutInflater = activity.getLayoutInflater();
resources = activity.getResources();
tabhost.setup(activity.getBaseContext(),
activity.getSupportFragmentManager(), realContentid);
tabhost.setOnTabChangedListener(this);
} public View newTabView(int layoutid, int backgroung, String title) {
TextView tv = (TextView) layoutInflater.inflate(layoutid, null);
tv.setText(title);
Drawable draw = resources.getDrawable(backgroung);
tv.setCompoundDrawablesWithIntrinsicBounds(null, draw, null, null);
return tv;
} public void AddTab(NsFragmentItem item) {
tabs.add(item);
fragmentTabHost.addTab(fragmentTabHost.newTabSpec(item.tag)
.setIndicator(item.view), item.fragment, null);
} public NsFragmentItem getTabByTag(String tag) {
for (NsFragmentItem item : tabs) {
if (item.tag == tag) {
return item;
}
}
return null;
} @Override
public void onTabChanged(String tabId) {
NsFragmentItem item = getTabByTag(tabId);
if (item != null) {
if (preItem != null) {
preItem.view.setBackgroundResource(preItem.upStyle);
}
preItem = item;
item.view.setBackgroundResource(item.downStyle);
}
} public static class NsFragmentItem {
public View view;
public String tag;
public Class<?> fragment;
public int downStyle;
public int upStyle;
}
}
下面就是示例,如何使用上面的库
package com.example.oldtab; import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.ActionBarActivity;
import android.view.View; import com.example.oldtab.NsFragmentTabHost.NsFragmentItem; public class Main extends ActionBarActivity { private static final String TAG_PERSON = "person";
private static final String TAG_GROUP = "group";
private static final String TAG_SETTING = "setting"; private NsFragmentTabHost nsTabHost;
private FragmentTabHost tabHost; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
nsTabHost = new NsFragmentTabHost(tabHost, this, R.id.realtabcontent);
View i1 = nsTabHost.newTabView(R.layout.tabitem,
R.drawable.ic_action_person, "个人");
View i2 = nsTabHost.newTabView(R.layout.tabitem,
R.drawable.ic_action_group, "集体");
View i3 = nsTabHost.newTabView(R.layout.tabitem,
R.drawable.ic_action_settings, "设置");
NsFragmentItem item1 = new NsFragmentItem();
item1.view = i1;
item1.tag = TAG_PERSON;
item1.fragment = PersonFragment.class;
item1.downStyle = R.drawable.tab_spec_down;
item1.upStyle = R.drawable.tab_spec_nomal;
NsFragmentItem item2 = new NsFragmentItem();
item2.view = i2;
item2.tag = TAG_GROUP;
item2.fragment = GroupFragment.class;
item2.downStyle = R.drawable.tab_spec_down;
item2.upStyle = R.drawable.tab_spec_nomal;
NsFragmentItem item3 = new NsFragmentItem();
item3.view = i3;
item3.tag = TAG_SETTING;
item3.fragment = SettingFragment.class;
item3.downStyle = R.drawable.tab_spec_down;
item3.upStyle = R.drawable.tab_spec_nomal;
nsTabHost.AddTab(item1);
nsTabHost.AddTab(item2);
nsTabHost.AddTab(item3);
}
}