Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a fragment_layout with nested layouts;

XML
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/bac_picture"
            android:alpha="0.55"
            android:layout_height="190dp"
            android:layout_width="match_parent"
            android:background="@drawable/background_image"
            android:contentDescription="@string/home_img_description"/>

        <!-- Followers, following, and posts statistics -->
        <TextView
            android:id="@+id/followers"
            android:text="@string/followers"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"
            android:textStyle="bold"
            android:textAlignment="center"
            android:textAllCaps="true"
            style="@style/ShadowText"
            android:gravity="center_horizontal"

            android:removed="#8d6e63"
            android:textColor="@color/primary_material_light"
            android:layout_below="@id/bac_picture"
            android:layout_width="130dp"
            android:layout_height="40dp" />

        <TextView
            android:id="@+id/following"

            android:text="@string/following"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"
            android:textStyle="bold"
            android:textAlignment="gravity"
            android:textAllCaps="true"
            style="@style/ShadowText"
            android:gravity="center_horizontal"

            android:removed="#795548"
            android:textColor="@color/primary_material_light"
            android:layout_below="@id/bac_picture"
            android:layout_toRightOf="@id/followers"
            android:layout_width="130dp"
            android:layout_height="40dp" />

        <TextView
            android:id="@+id/posts"

            android:text="@string/posts"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"
            android:textStyle="bold"
            android:textAlignment="gravity"
            android:textAllCaps="true"
            style="@style/ShadowText"
            android:gravity="center_horizontal"

            android:removed="#8d6e63"
            android:textColor="@color/primary_material_light"
            android:layout_below="@id/bac_picture"
            android:layout_toRightOf="@id/following"
            android:layout_width="130dp"
            android:layout_height="40dp" />

        <!-- TabHost Views -->
        <TabHost
            android:id="@android:id/tabhost"
            android:layout_below="@id/followers"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

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

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

                <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="fill_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"/>

            </LinearLayout>

        </TabHost>

    </RelativeLayout>

</FrameLayout>


I have seperate layouts for each of the tabs that are included in the above layout; home_tab_layout

XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".DeviceFragment"
    android:removed="@color/primary_material_light">

    <ScrollView
        android:id="@+id/userContent"
        android:removed="@color/hobbyist_light"
        android:layout_below="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            <TextView
                android:text="@string/user_info"
                style="@style/tabDimensions"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            </TextView>

        </LinearLayout>

    </ScrollView>

</LinearLayout>



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

    <ListView
        android:id="@+id/hometab2"
        android:divider="@color/hobbyist_light"
        android:dividerHeight="0.2dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:removed="@color/hobbyist_accent" />

</LinearLayout>



I have a home class that extends Fragment and in the onCreateView method, I try to inflate the the fragment with a view that includes everything in the fragment_layout as well as the respective view for each of the Tabs.

I have already created a TabAdapter class.
I'm currently having trouble with the onCreateView method in the home class.

I would like to know how to go about handling this.


Here's my home class

Java
public class home extends Fragment{
   public static final String IMAGE_RESOURCE_ID = "iconResourceID";
   public static final String ITEM_NAME = "itemName";
   private String mParam1;
   private String mParam2;
   ImageView ivIcon;
   TextView tvItemName;
   public home(){}

     @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        //return inflater.inflate(R.layout.fragment_home, container, false);
        inflater.inflate(R.layout.fragment_home,container,false);
        return (LinearLayout)inflater.inflate(R.layout.home_about_tab,container,false);
    }
}



I have a TabHost class that makes use of a HomeTabAboutMe, and HomeTabHobbies classes which are made for the xml tab layouts.


Here's the HomeTabAboutMe class
Java
public class HomeTabAboutMe extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
        View rootView = inflater.inflate(R.layout.home_about_tab,container, false);
        return rootView;
    }
}


Here's the HomeTabHobbies class
Java
public class HomeTabHobbies extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
        View rootView = inflater.inflate(R.layout.home_hobbies_tab,container, false);
        return rootView;
    }
}



Finally here's the TabHost class
Java
public class TabHost extends Fragment{
    private FragmentTabHost mTabHost;
    public static final String IMAGE_RESOURCE_ID = "iconResourceID";//used to be private
    public static final String ITEM_NAME = "itemName";

    //Mandatory constructor
    public TabHost(){}

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View rootView = inflater.inflate(R.layout.fragment_explore,container,false);
        mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
        mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
        mTabHost.addTab(mTabHost.newTabSpec("home_about_tab").setIndicator("ABOUT ME"),HomeTabAboutMe.class,null);
        mTabHost.addTab(mTabHost.newTabSpec("home_hobbies_tab").setIndicator("HOBBIES"),HomeTabHobbies.class,null);
        return rootView;
    }
}


Thanks.
Posted
Updated 17-Jun-15 18:57pm
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900