简体   繁体   English

在片段内添加带有活动的标签主机

[英]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>

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. 据我了解-您想将具有不同activities TabActivity (不建议使用wich)放入主活动的根Fragment这种方法绝对是错误的。

One way to implement what you need is: 实现您所需的一种方法是:

  1. create one FragmentActivity . 创建一个FragmentActivity
  2. put TabWidget into root layout of your FragmentActivity . 将TabWidget放入FragmentActivity根布局中。
  3. then put different Fragments into you TabWidget's tabs. 然后将不同的Fragments放入TabWidget的标签中。

You can look at my similar answer with code examples into another theme (link) . 您可以通过将代码示例转换为另一个主题来查看我的类似答案(链接)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM