nabiullinas
nabiullinas

Reputation: 1245

add tab host with activities inside fragment

Good day. I have some problem. I have app with listview at left and detail view at right. In right view I have a fragment with tab host. But I also want to add activities for all tabs. For example: I have clients list at left. At right I have tabs: "clients comments", "clients photos", "clients Info" In clients comments I need in activity with comments for this client, and with possibility to add new comment. I have already made a list view and detail, but I have problems with integration tab hosts into it. So what I have. Here My code of detail fragment

 public class ItemDetailFragment extends Fragment {

        public static final String ARG_ITEM_ID = "item_id";

        DummyContent.DummyItem mItem;
        private Activity lo_parentAct;



        public ItemDetailFragment() {
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            if (getArguments().containsKey(ARG_ITEM_ID)) {
                mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
            }
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_item_detail, container, false);
            if (mItem != null) {
                Intent lv_intent;
            //    ((TextView) rootView.findViewById(R.id.item_detail)).setText(mItem.content);
                TabHost tabHost=(TabHost)rootView.findViewById(R.id.tabHost);
                tabHost.setup();

                TabSpec spec1=tabHost.newTabSpec("Tab 1");
                spec1.setIndicator("Общая информация");
                lo_parentAct = this.getActivity();
                lv_intent = new Intent(lo_parentAct, ClientInfoActivity.class);

                TabSpec spec2=tabHost.newTabSpec("Tab 2");
                spec2.setIndicator("Заметки");
                lv_intent = new Intent(lo_parentAct, ClientCommentsActivity.class);

                TabSpec spec3=tabHost.newTabSpec("Tab 3");
                spec3.setIndicator("Фото");
                lv_intent = new Intent(lo_parentAct, ClientPhotosActivity.class);


                tabHost.addTab(spec1);
                tabHost.addTab(spec2);
                tabHost.addTab(spec3);

            }
            return rootView;
        }
    }

An the layout

    <TabHost android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/tabHost"
            xmlns:android="http://schemas.android.com/apk/res/android"
            >
        <TabWidget
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@android:id/tabs"
            />
             <FrameLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@android:id/tabcontent"
             >
                 <LinearLayout
                     android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/tab1"
                    android:orientation="vertical"
                    android:paddingTop="60px"
                >
             <TextView  
            android:layout_width="fill_parent" 
            android:layout_height="100px" 
            android:text="This is tab1"
            android:id="@+id/txt1"
            />    

     </LinearLayout>

     <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/tab2"
            android:orientation="vertical"
            android:paddingTop="60px"
             >
         <TextView  
                android:layout_width="fill_parent" 
                android:layout_height="100px" 
                android:text="This is tab 2"
                android:id="@+id/txt2"
                />

     </LinearLayout>

      <LinearLayout
     android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/tab3"
    android:orientation="vertical"
    android:paddingTop="60px"
     >
             <TextView  
            android:layout_width="fill_parent" 
            android:layout_height="100px" 
            android:text="This is tab 3"
            android:id="@+id/txt3"
            />

     </LinearLayout>
     </FrameLayout>

    </TabHost>

Upvotes: 0

Views: 3302

Answers (1)

ashakirov
ashakirov

Reputation: 12350

add tab host with activities inside fragment

You can't put activities into fragments! As I understand - you want to put TabActivity (wich is deprecated) with different activities into your main activity's root Fragment - this way is absolutely wrong.

One way to implement what you need is:

  1. create one FragmentActivity.
  2. put TabWidget into root layout of your FragmentActivity.
  3. then put different Fragments into you TabWidget's tabs.

You can look at my similar answer with code examples into another theme (link).

Upvotes: 1

Related Questions