还是从Tab的几个组成部分来分析:Tab标签页(ActionBar.Tab),Tab所切换内容部分content( Fragment实现)

1、新建每个Fragment, layout,Tab的内容部分

相应地在Activity布局中Fragment位置,android:id="@+id/fragment_place"

2、Activity中获得ActionBar, 设置Tab模式

ActionBar bar = getActionBar();

bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

3、新建Tab标签页

ActionBar.Tab tabA = bar.newTab().setText("A Tab");

4、声明Tab(Fragment), 并添加监听器。让Tab标签页(ActionBar.Tab),内容(Fragment),监听事件关联,在监听器的事件onTabSelectedonTabUnselected 进行Tab内容 Fragment切换。

Fragment fragmentA =newAFragmentTab();

tabA.setTabListener(newMyTabsListener(fragmentA));

5、添加Tab到actionBar中

bar.addTab(tabA);

Fragment:

public class AFragmentTab extends Fragment {

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState)

{

return inflater.inflate(R.layout.fragment_a, container, false);

}

}

Fragment布局 fragment_a.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

main.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/fragment_place"

android:layout_width="match_parent"

android:layout_height="wrap_content">

TabsActivity

public class TabsActivity extends FragmentActivity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

ActionBar bar = getActionBar();

bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

ActionBar.Tab tabA = bar.newTab().setText("A Tab");

ActionBar.Tab tabB = bar.newTab().setText("B Tab");

ActionBar.Tab tabC = bar.newTab().setText("C Tab");

Fragment fragmentA = new AFragmentTab();

Fragment fragmentB = new BFragmentTab();

Fragment fragmentC = new CFragmentTab();

tabA.setTabListener(new MyTabsListener(fragmentA, this));

tabB.setTabListener(new MyTabsListener(fragmentB,this));

tabC.setTabListener(new MyTabsListener(fragmentC,this));

bar.addTab(tabA);

bar.addTab(tabB);

bar.addTab(tabC);

}

protected class MyTabsListener implements ActionBar.TabListener {

private Fragment fragment;

private FragmentActivity mActivity;

FragmentTransaction fmt;

public MyTabsListener(Fragment fragment,FragmentActivity activity) {

this.fragment = fragment;

this.mActivity = activity;

}

@Override

public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {

// TODO Auto-generated method stub

}

@Override

public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {

// TODO Auto-generated method stub

FragmentTransaction fmt = mActivity.getSupportFragmentManager().beginTransaction();

fmt.add(R.id.fragment_place,fragment);

fmt.commit();

}

@Override

public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {

// TODO Auto-generated method stub

FragmentTransaction fmt = mActivity.getSupportFragmentManager().beginTransaction();

fmt.remove(fragment);

fmt.commit();

}

}

}

效果:

6597760259960525077.png

[转]http://blog.163.com/ppy2790@126/blog/static/103242241201386207499/

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐