FragmentTabHost未显示选项卡内容

时间:2014-09-25 08:29:57

标签: android

我在程序中使用FragmentTabHost显示子片段(片段中的片段)时遇到问题。标签主机显示完美,但其内容未显示...

首先,介绍类:

Order.java:

public class Order extends Fragment{
    private View view;
    private FragmentTabHost orderMenu;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        view = inflater.inflate(R.layout.order, container, false);
        setLayout();
        return view;
    }

    public void setLayout(){
        orderMenu = (FragmentTabHost)view.findViewById(R.id.order_tabhost);
        orderMenu.setup(getActivity(), getChildFragmentManager(), R.id.order_tabcontent);

        Bundle testArg1 = new Bundle();
        testArg1.putString("tag", "t1");

        Bundle testArg2 = new Bundle();
        testArg2.putString("tag", "t2");

        Bundle testArg3 = new Bundle();
        testArg3.putString("tag", "t3");

        orderMenu.addTab(orderMenu.newTabSpec("t1").setIndicator("fruit"), OrderMenuList.class, testArg1);
        orderMenu.addTab(orderMenu.newTabSpec("t2").setIndicator("bird"), OrderMenuList.class, testArg2);
        orderMenu.addTab(orderMenu.newTabSpec("t3").setIndicator("meat"), OrderMenuList.class, testArg3);
    }   

}

order.xml

<?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.support.v4.app.FragmentTabHost
        android:id="@+id/order_tabhost"
        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" >
            </TabWidget>

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

            </FrameLayout>

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

            </FrameLayout>
        </LinearLayout>
    </android.support.v4.app.FragmentTabHost>


</LinearLayout>

OrderMenuList.java

public class OrderMenuList extends Fragment{
    private final String TAG = this.getClass().getName();

    private View view;
    private ListView menuList;

    private String[] menu1 = {"apple", "orange", "banana", "melon"};
    private String[] menu2 = {"duck", "chicken", "turkey"};
    private String[] menu3 = {"steak", "pork"};

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        view = inflater.inflate(R.layout.menu_list, container, false);
        Log.d(TAG, ""+this.getArguments().getString("tag"));
        setLayout();
        return view;
    }

    public void setLayout(){
        menuList = (ListView)view.findViewById(R.id.menu_list);
        ArrayAdapter<String> adapter = null;
        if (this.getArguments()!=null){
            if (this.getArguments().getString("tag").equals("t1")){
                adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, menu1);
            } else if (this.getArguments().getString("tag").equals("t2")){
                adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, menu2);
            } else if (this.getArguments().getString("tag").equals("t3")){
                adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, menu3);
            }
        }
        menuList.setAdapter(adapter);
        Log.d(TAG, ""+adapter.getCount());
    }
}

menu_list.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/menu_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>


</LinearLayout>

我也尝试过简单地使用textview而不是listview来测试它,但它也不起作用。所以,对于现在的舞台,我的目标很简单........显示片段中的标签内容!!!!!!

1 个答案:

答案 0 :(得分:2)

FrameLayout android:id="@android:id/tabcontent"android:layout_height="match_parent" FragmentTabHost,因此它占据了所有可用的高度。但是,您设置的R.id.order_tabcontent FrameLayout位于下方,因此您无法看到它。

删除其中一个FragmentTabHost并将{{1}}设置为另一个。