如何在片段内实现tabhost?

时间:2014-01-08 02:59:13

标签: android android-fragments

我尝试将标签主机放入框架中,但在运行代码时,tabhost无法显示

类扩展片段的java代码:

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_host_layout,
            container, false);

    setHasOptionsMenu(true);

    TabHost mTabHost = (TabHost) rootView.findViewById(R.id.tabhost);


    TabSpec photospec = mTabHost.newTabSpec("Search");
    photospec.setIndicator("Search", getResources().getDrawable(R.drawable.action_search));
    Intent searchIntent = new Intent(getActivity(), HostViewIntroduce.class);
    photospec.setContent(searchIntent);

    TabSpec songspec = mTabHost.newTabSpec("Favorites");
    // setting Title and Icon for the Tab
    songspec.setIndicator("Favorites", getResources().getDrawable(R.drawable.action_search));
    Intent favoriteIntent = new Intent(getActivity(), HostViewTips.class);
    songspec.setContent(favoriteIntent);

    return rootView;

}

xml代码:

 <LinearLayout
    android:id="@+id/thumbnail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/relativeLayout1"
    android:layout_marginRight="5dip"
    android:background="@drawable/image_bg"
    android:padding="3dip" >

    <ImageView
        android:id="@+id/userAvatar"
        android:layout_width="wrap_content"
        android:layout_height="89dp"
        android:src="@drawable/test_avatar" />
</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/thumbnail"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/thumbnail"
    android:layout_below="@+id/relativeLayout1"
    android:layout_toRightOf="@+id/thumbnail"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="soloco bolo"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/ImageView01"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@drawable/description_section_icon" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="123456"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@drawable/ic_menu_add" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button1"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="123dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button" />

        <Button
            android:id="@+id/button2"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="125dp"
            android:layout_height="wrap_content"
              android:layout_weight="1"
            android:text="Button" />
    </LinearLayout>

</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/thumbnail" >

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:text="Đà Nẵng"
        android:textAppearance="?android:attr/textAppearanceLarge" />

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

        <TextView
            android:id="@+id/textView4"
            android:layout_width="match_parent"
            android:layout_height="68dp"
            android:gravity="center_vertical"
            android:text="Activate"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:text="6/1/2013-7/1/2013"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/relativeLayout2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:orientation="vertical" >

    <TabHost
        android:id="@+id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.30" >

        <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="92dp" >
            </TabWidget>

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

                <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical" >

                    <ListView
                        android:id="@+id/tab1list_place_list"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:divider="#b5b5b5"
                        android:dividerHeight="1dp"
                        android:listSelector="@drawable/list_selector" >
                    </ListView>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical" >

                    <ListView
                        android:id="@+id/tab2list_place_list"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:divider="#b5b5b5"
                        android:dividerHeight="1dp"
                        android:listSelector="@drawable/list_selector" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical" >

                    <ListView
                        android:id="@+id/tab3list_place_list"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:divider="#b5b5b5"
                        android:dividerHeight="1dp"
                        android:listSelector="@drawable/list_selector" />
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>
</LinearLayout>

这是屏幕截图:

https://dl.dropboxusercontent.com/u/37599516/Untitled.png

这正是我想要的

https://dl.dropboxusercontent.com/u/37599516/Untitled.png

1 个答案:

答案 0 :(得分:1)

试试这个:

 public class AndroidViewPagerActivity extends FragmentActivity {

    ViewPager mViewPager;
TabsAdapter mTabsAdapter;

 public void setActionBarTitle(String title) {
    getActionBar().setTitle(title); 
 }

 /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mViewPager = new ViewPager(this);
    mViewPager.setId(R.id.pager);
    setContentView(mViewPager);

    final ActionBar bar = getActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);

    mTabsAdapter = new TabsAdapter(this, mViewPager);
    mTabsAdapter.addTab(bar.newTab().setText("Header"),
            FragmentSales1.class, null);
    mTabsAdapter.addTab(bar.newTab().setText("Detail"),
            Fragment2.class, null);

    if (savedInstanceState != null) {
        bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
    }

}

@Override
protected void onSaveInstanceState(Bundle outState) {
    //super.onSaveInstanceState(outState);
    outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
}

public static class TabsAdapter extends FragmentPagerAdapter
    implements ActionBar.TabListener, ViewPager.OnPageChangeListener {

    private final Context mContext;
    private final ActionBar mActionBar;
    private final ViewPager mViewPager;
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

    static final class TabInfo {
        private final Class<?> clss;
        private final Bundle args;

        TabInfo(Class<?> _class, Bundle _args) {
            clss = _class;
            args = _args;
        }
    }

    public TabsAdapter(FragmentActivity activity, ViewPager pager) {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mActionBar = activity.getActionBar();
        mViewPager = pager;
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
    }

    public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
        TabInfo info = new TabInfo(clss, args);
        tab.setTag(info);
        tab.setTabListener(this);
        mTabs.add(info);
        mActionBar.addTab(tab);
        notifyDataSetChanged();
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageSelected(int position) {
        // TODO Auto-generated method stub
        mActionBar.setSelectedNavigationItem(position);
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        Object tag = tab.getTag();
        for (int i=0; i<mTabs.size(); i++) {
            if (mTabs.get(i) == tag) {
                mViewPager.setCurrentItem(i);
            }
        }
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public Fragment getItem(int position) {
        TabInfo info = mTabs.get(position);
        return Fragment.instantiate(mContext, info.clss.getName(), info.args);
    }

    @Override
    public int getCount() {
        return mTabs.size();
    }

}



public class TabsPagerAdapterForSalesOrder extends FragmentPagerAdapter {

public TabsPagerAdapterForSalesOrder(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int index) {

    switch (index) {
    case 0:
        // Header fragment activity
        return new FragmentSalesOrderHeader();
    case 1:
        // Detail fragment activity
        return new FragmentSalesOrderDetail();
    }

    return null;
}

@Override
public int getCount() {
    // get item count - equal to number of tabs
    return 2;
}

 }

<强>更新 tab.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>

注意:

为每个标签创建两个布局。

希望这会有所帮助。