public class TabsActivity extends Activity {
protected static final String tag = "maps";
Context context = null;
LocalActivityManager manager = null;
ViewPager pager = null;
TabHost tabHost = null;
protected int currentItemId;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_tabs_pager);
manager = new LocalActivityManager(this, true);
manager.dispatchCreate(savedInstanceState);
context = TabsActivity.this;
initiaTabHost();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
private View getView(String id, Intent intent) {
return manager.startActivity(id, intent).getDecorView();
}
public void initiaTabHost() {
tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
tabHost.setup(manager);
Intent it1 = new Intent(context, WifiSearchAcitivity.class);
it1.putExtra("name", WifiSearchAcitivity.tag);
Intent it2 = new Intent(context, MapActivity.class);
it2.putExtra("name", MapActivity.tag);
Intent it3 = new Intent(context, MyPreferenceActivity.class);
it3.putExtra("name", MyPreferenceActivity.tag);
tabHost.addTab(tabHost.newTabSpec("列表").setIndicator("列表")
.setContent(it1));
tabHost.addTab(tabHost.newTabSpec("地图").setIndicator("地图")
.setContent(it2));
tabHost.addTab(tabHost.newTabSpec("设置").setIndicator("设置")
.setContent(it3));
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
log(tag, "onTabChange called id= " + tabId);
if (WifiSearchAcitivity.tag.equals(tabId)) {
pager.setCurrentItem(0);
} else if (MapActivity.tag.equals(tabId)) {
pager.setCurrentItem(1);
} else if (MyPreferenceActivity.tag.equals(tabId)) {
pager.setCurrentItem(2);
}
}
});
pager = (ViewPager) findViewById(R.id.viewpage);
final ArrayList<View> list = new ArrayList<View>();
list.add(getView(WifiSearchAcitivity.tag, it1));
list.add(getView(MapActivity.tag ,it2));
list.add(getView(MyPreferenceActivity.tag ,it3));
pager.setAdapter(new PagerAdapter() {
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
/*
* log(tag, "destroyItem called arg1="+arg1); ViewPager
* pViewPager = ((ViewPager) arg0);
* pViewPager.removeView(list.get(arg1));
*/
}
@Override
public void finishUpdate(View arg0) {
log(tag, "finishupdate called");
}
@Override
public int getCount() {
// log(tag, "getcount called");
return list.size();
}
@Override
public Object instantiateItem(ViewGroup arg0, int arg1) {
log(tag, " instantiateItem called arg1= " + arg1);
ViewPager pViewPager = ((ViewPager) arg0);
if (pViewPager.getChildCount() == 0) {// pViewPager.removeAllViews();
for (View view : list) {
pViewPager.addView(view);
}
}
return list.get(arg1);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// log(tag, "isViewfromObject called " + (arg0 == arg1));
return arg0 == arg1;
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {
}
});
pager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
log(tag, "onPageChangeListener called " + arg0);
currentItemId = arg0;
tabHost.setCurrentTab(arg0);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
}