使用Fragment实现类似Tab的需求
大家都知道可以使用TabHost来实现, 不过这种实现已经是被deprecated, 其实是可以通过Fragment来实现, 不过如果是Fragment的话好像只能每次new一个fragment,这样感觉不太好, 按常理如果是以前有创建过fragment,下一次应该还是显示那个fragment实例。 通过google得知可以通过FragmentTransaction的attach和detach来实现。 下面贴下代码。
import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.widget.TabHost;?public class MainActivity extends FragmentActivity {???? ???TabHost tHost;???? @Override??? public void onCreate(Bundle savedInstanceState) {???? ???super.onCreate(savedInstanceState);???? ???setContentView(R.layout.activity_main);???? ???????? ???tHost = (TabHost) findViewById(android.R.id.tabhost);???? ???tHost.setup();???? ???????? ???/** Defining Tab Change Listener event. This is invoked when tab is changed */???? ???TabHost.OnTabChangeListener tabChangeListener = new TabHost.OnTabChangeListener() {???? ?? ?? ?? ?? ?? ?? ?????? ?? ?? ?? ?? ?? ?? ?@Override???? ?? ?? ?? ?? ?? ?? ?public void onTabChanged(String tabId) {???? ?? ?? ?? ?? ?? ?? ?? ?? ???android.support.v4.app.FragmentManager fm =? ?getSupportFragmentManager();???? ?? ?? ?? ?? ?? ?? ?? ?? ???AndroidFragment androidFragment = (AndroidFragment) fm.findFragmentByTag("android");???? ?? ?? ?? ?? ?? ?? ?? ?? ???AppleFragment appleFragment = (AppleFragment) fm.findFragmentByTag("apple");???? ?? ?? ?? ?? ?? ?? ?? ?? ???android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();???? ?? ?? ?? ?? ?? ?? ?? ?? ???????? ?? ?? ?? ?? ?? ?? ?? ?? ???/** Detaches the androidfragment if exists */???? ?? ?? ?? ?? ?? ?? ?? ?? ???if(androidFragment!=null)???? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? ft.detach(androidFragment);???? ?? ?? ?? ?? ?? ?? ?? ?? ???????? ?? ?? ?? ?? ?? ?? ?? ?? ???/** Detaches the applefragment if exists */???? ?? ?? ?? ?? ?? ?? ?? ?? ???if(appleFragment!=null)???? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? ft.detach(appleFragment);???? ?? ?? ?? ?? ?? ?? ?? ?? ???????? ?? ?? ?? ?? ?? ?? ?? ?? ???/** If current tab is android */???? ?? ?? ?? ?? ?? ?? ?? ?? ???if(tabId.equalsIgnoreCase("android")){???? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? ????? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? if(androidFragment==null){? ?? ?? ?? ?? ? ???? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?/** Create AndroidFragment and adding to fragmenttransaction */???? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?ft.add(R.id.realtabcontent,new AndroidFragment(), "android");? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ????? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? }else{???? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?/** Bring to the front, if already exists in the fragmenttransaction */???? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?ft.attach(androidFragment);? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ????? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? }???? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? ????? ?? ?? ?? ?? ?? ?? ?? ?? ???}else{? ?? ???/** If current tab is apple */???? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? if(appleFragment==null){???? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?/** Create AppleFragment and adding to fragmenttransaction */???? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?ft.add(R.id.realtabcontent,new AppleFragment(), "apple");? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ????? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? }else{???? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?/** Bring to the front, if already exists in the fragmenttransaction */???? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?ft.attach(appleFragment);? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ????? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? }???? ?? ?? ?? ?? ?? ?? ?? ?? ???}???? ?? ?? ?? ?? ?? ?? ?? ?? ???ft.commit();? ?? ?? ?? ?? ?? ?? ?? ?? ?? ??????? ?? ?? ?? ?? ?? ?? ?}???? ?? ?? ?? ? };???? ?? ?? ?? ? ????? ?? ?? ?? ? ????? ?? ?? ?? ? /** Setting tabchangelistener for the tab */???? ?? ?? ?? ? tHost.setOnTabChangedListener(tabChangeListener);???? ?? ?? ?? ? ????? ?? ?? ?? ? /** Defining tab builder for Andriod tab */???? ???TabHost.TabSpec tSpecAndroid = tHost.newTabSpec("android");???? ???tSpecAndroid.setIndicator("Android",getResources().getDrawable(R.drawable.android));? ?? ??????? ???tSpecAndroid.setContent(new DummyTabContent(getBaseContext()));???? ???tHost.addTab(tSpecAndroid);???? ???????? ???????? ???/** Defining tab builder for Apple tab */???? ???TabHost.TabSpec tSpecApple = tHost.newTabSpec("apple");???? ???tSpecApple.setIndicator("Apple",getResources().getDrawable(R.drawable.apple));? ?? ??????? ???tSpecApple.setContent(new DummyTabContent(getBaseContext()));???? ???tHost.addTab(tSpecApple);???? ?????????}? ????? ?? ??}