再次添加标签到android中的标签片段活动

时间:2014-08-08 08:54:07

标签: android android-fragments android-tabhost android-tabs

我有一个应用程序,它有导航类型固定标签+滑动。我使用两个片段创建了两个选项卡,片段1和片段2.它工作正常。现在我再次需要为片段1创建3个选项卡。如何实现?

这是我的MainActivity:

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;// Tab titles
private String[] tabs = { "Flights","My Bookings" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
        .setTabListener(this));
}

/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

@Override
public void onPageSelected(int position) {
    // on changing the page
    // make respected tab selected
    actionBar.setSelectedNavigationItem(position);
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}

@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}

}

这是我的TabsPagerAdapter类:

public class TabsPagerAdapter extends FragmentPagerAdapter {

public TabsPagerAdapter(FragmentManager fm) {
    super(fm);
    // TODO Auto-generated constructor stub
}

@Override
public Fragment getItem(int index) {
    // TODO Auto-generated method stub
    switch (index) {
    case 0:
            return new Flight();
    case 1:
            return new Hotel();


    }
    return null;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return 2;
}

}

这是我的mainactivity.xml文件

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>

然后我的应用程序中有两个片段活动Flight和Hotel。现在,我再次需要为片段活动航班添加标签。怎么能实现?有人可以帮帮我......

修改:这是来自网络的图片。这就是我想要的......

enter image description here

2 个答案:

答案 0 :(得分:2)

添加另一个片段类如下:

@Override
public Fragment getItem(int index) {

// TODO Auto-generated method stub
switch (index) {
case 0:
        return new Flight();
case 1:
        return new Hotel();
case 2:
       return new Thirdfragment();
}
return null;


 }

 @Override
 public int getCount() {
// TODO Auto-generated method stub
  return 3;
  }

并且不要忘记在MainActivity类中添加第三个选项卡        private String [] tabs = {“Flights”,“My Bookings”,ThirdFragment};

答案 1 :(得分:1)

必须将您的航班片段布局更改为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
android:descendantFocusability="blocksDescendants" 
 >

<TabHost

    android:id="@+id/TabHost01"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
           >

            <anylayout
                android:id="@+id/tab_1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                 >
            </anylayout>

            <anylayout
                android:id="@+id/tab_2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:dividerHeight="3dp" >


            <anylayout
                android:id="@+id/tab_3"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

          </anylayout>
        </FrameLayout>
    </LinearLayout>
 </TabHost>

 </LinearLayout>

并在onCreatView中:

mTabHost = (TabHost) v.findViewById(R.id.TabHost01);

mTabHost.setup();

mTabHost.addTab(mTabHost.newTabSpec("tab_1")
                .setIndicator("View1_Child1")
                .setContent(R.id.tab_1));

mTabHost.addTab(mTabHost.newTabSpec("tab_2")
                .setIndicator("View2_Child2")
                .setContent(R.id.tab_2));