Simon
Simon

Reputation: 481

FragmentTabHost not showing tab content

I have a problem on displaying child fragment(fragment in fragment) with FragmentTabHost in my program. The tab host is displayed perfectly but its content does not been shown...

First, introduce the classes:

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>

I have also tried simply put a textview instead of listview to test it but it also does not work. So, for now stage, my aim is very simple........show the tab content in the fragment!!!!!!

Upvotes: 1

Views: 1560

Answers (1)

Anton Savin
Anton Savin

Reputation: 41321

You have FrameLayout with android:id="@android:id/tabcontent" and android:layout_height="match_parent", so it occupies all available height. But you setup your FragmentTabHost with R.id.order_tabcontent which resides below, so you don't see it.

Remove one of FrameLayout's and setup FragmentTabHost with another.

Upvotes: 2

Related Questions