首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在片段内添加FragmentTabHost

在片段内添加FragmentTabHost
EN

Stack Overflow用户
提问于 2015-09-07 19:13:43
回答 1查看 626关注 0票数 2

我有一个应用程序与一个从片断创建的侧边菜单。

当我想在其中一个片段中创建选项卡时,问题就来了。

我已经尝试了很多例子,但对我来说都不起作用。

fragment_home.xml

<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/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"
        android:layout_weight="0"
        android:orientation="horizontal"
        ></TabWidget>
    <FrameLayout 
        android:id="@android:id/tabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        ></FrameLayout>

</LinearLayout>

FragmentHome.java

    public class FragmentInicio extends Fragment {

    private FragmentTabHost mTabHost;

    public FragmentInicio() {}


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        try{
            //HERE TABHOST????

        }catch(Exception e){
            Log.e("UDI", e.getMessage());
        }

        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        Log.d("EVENT", "OnCreate");

    }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onViewCreated(view, savedInstanceState);
    }


    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        ((HomeActivity) activity).onSectionAttached(1);
    }
}

我能做什么?

示例:

http://webdemo.com.es/sample2.jpg

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2018-09-18 04:07:33

我知道我来晚了,但这可能会对别人有帮助。

要在片段中使用FragmentTabHost,您可以这样做。

创建布局资源文件

fragment_tab_host.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    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_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"/>
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0"/>
        <FrameLayout
            android:id="@+id/realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
    </LinearLayout>
</android.support.v4.app.FragmentTabHost>

id为realtabcontent的FrameLayout将显示当前选项卡内容,在本例中为FirstFragment、SecondFragment和ThirdFragment

TabHostWidgetFragment.java代码将是

    public class TabHostWidgetFragment extends Fragment {

    private static final String TAG = TabHostWidgetFragment.class.getSimpleName();

    public TabHostWidgetFragment() { }

    private FragmentTabHost fragmentTabHost;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        Log.i(TAG, "onCreateView: ");
        return inflater.inflate(R.layout.fragment_frag_tab_host_widget, container, false);
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.i(TAG, "onViewCreated: ");
        fragmentTabHost = view.findViewById(android.R.id.tabhost);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.i(TAG, "onActivityCreated: ");

        fragmentTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
        fragmentTabHost.addTab(fragmentTabHost.newTabSpec("First Tab").setIndicator("First Tab"), FirstFragment.class, null);
        fragmentTabHost.addTab(fragmentTabHost.newTabSpec("Second Tab").setIndicator("Second Tab"), SecondFragment.class, null);
        fragmentTabHost.addTab(fragmentTabHost.newTabSpec("Third Tab").setIndicator("Third Tab"), ThirdFragment.class, null);

    }


    public class FirstFragment extends Fragment {

        private static final String TAG = FirstFragment.class.getSimpleName();
        public FirstFragment() { }

        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                                 @Nullable Bundle savedInstanceState) {
            Log.i(TAG, "onCreateView: ");
            return inflater.inflate(R.layout.fragment_first, container, false);
        }

        @Override
        public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            Log.i(TAG, "onViewCreated: ");
        }

    }


    public class SecondFragment extends Fragment {

        private static final String TAG = SecondFragment.class.getSimpleName();
        public SecondFragment() { }

        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                                 @Nullable Bundle savedInstanceState) {
            Log.i(TAG, "onCreateView: ");
            return inflater.inflate(R.layout.fragment_second, container, false);
        }


        @Override
        public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            Log.i(TAG, "onViewCreated: ");
        }

    }


     public class ThirdFragment extends Fragment {

        private static final String TAG = ThirdFragment.class.getSimpleName();
        public ThirdFragment() { }


        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                                 @Nullable Bundle savedInstanceState) {
            Log.i(TAG, "onCreateView: ");
            return inflater.inflate(R.layout.fragment_third, container, false);
        }


        @Override
        public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            Log.i(TAG, "onViewCreated: ");
        }

    }


}

FristFragment、SecondFragment和ThirdFragment的布局资源

fragment_first.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/firstTab"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/nature1"/>
</LinearLayout>

fragment_second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/secondTab"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/nature2"/>
</LinearLayout>

fragment_third.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/thirdTab"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:scaleType="centerInside"
        android:src="@drawable/nature3"/>
</LinearLayout>

如果你想让它变得可滑动,那么在FrameLayout后面添加id为realtabcontent的ViewPager,如下所示,并实现viewPager逻辑。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    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_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"/>
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0"/>
        <FrameLayout
            android:id="@+id/realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0"/>
        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
    </LinearLayout>
</android.support.v4.app.FragmentTabHost>

而TabHostWidgetFragment.java类将是

创建单独的或内部的MyTabFactory.java类

public class MyTabFactory implements TabHost.TabContentFactory {

    private final Context context;

    public MyTabFactory(Context context) {
        this.context = context;
    }

    @Override
    public View createTabContent(String s) {
        View v = new View(context);
        v.setMinimumWidth(0);
        v.setMinimumHeight(0);
        return v;
    }
}

然后向tabhost添加标签,如下所示。这对于使用MyTabFactory的setContent来说是必不可少的。如果你像我上面在代码片段中提到的那样添加标签,就会有问题,那就是,它会添加两次片段,一次是因为TabHost,另一次是因为ViewPager。

fragmentTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("First Tab").setIndicator("First Tab").setContent(new MyTabFactory(getActivity())));
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("Second Tab").setIndicator("Second Tab").setContent(new MyTabFactory(getActivity())));
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("Third Tab").setIndicator("Third Tab").setContent(new MyTabFactory(getActivity())));

你可以使用android.support.v4.app.FragmentTabHostTabHost,两者的效果是一样的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32437307

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档