片段tabhost不在片段工作 [英] Fragment tabhost is not working in Fragment

查看:139
本文介绍了片段tabhost不在片段工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面这个turtorial的http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

在这里Homefragment我设置了fragmenttabhost,问题是标签的观点并没有显示

 公共类HomeFragment扩展片段{
    私人FragmentTabHost mTab​​Host;    公共HomeFragment(){}    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
            捆绑savedInstanceState){        查看rootView = inflater.inflate(R.layout.my_parent_fragment,集装箱,FALSE);        mTabHost =(FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
        mTabHost.setup(getActivity(),getFragmentManager(),android.R.id.tabcontent);        mTabHost.addTab(
                mTabHost.newTabSpec(TAB1)。setIndicator(发现,NULL)
                FragmentA.class,NULL);
        mTabHost.addTab(
                mTabHost.newTabSpec(TAB2)。setIndicator(SHOP,NULL)
                FragmentB.class,NULL);        返回rootView;
    }
}

FragmentA.java

 公共类碎裂扩展片段{    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
            捆绑savedInstanceState){        查看rootView = inflater.inflate(R.layout.fraga,集装箱,FALSE);
        Toast.makeText(getActivity(),发现,Toast.LENGTH_LONG).show();
        返回rootView;
    }}

fraga.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:背景=#FF0000
    >    <的TextView
        机器人:ID =@ + ID / txtLabel
         机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_centerInParent =真
        机器人:TEXTSIZE =16DP
        机器人:文字=发现/>< / RelativeLayout的>


解决方案

试试这个..

 公共类HomeFragment扩展片段{    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    清单<串GT; listDataHeader;
    HashMap的<字符串列表<串GT;> listDataChild;    私人FragmentTabHost tabHost;    公共HomeFragment(){}    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
            捆绑savedInstanceState){        tabHost =新FragmentTabHost(getActivity());
        tabHost.setup(getActivity(),getChildFragmentManager(),R.layout.my_parent_fragment);        捆绑ARG1 =新包();
        arg1.putInt(精氨酸为Frag1,1);
        tabHost.addTab(tabHost.newTabSpec(TAB1)。setIndicator(TAB1)),
                FragmentA.class,ARG1);        捆绑ARG2 =新包();
        arg2.putInt(精氨酸为Frag2,2);
        tabHost.addTab(tabHost.newTabSpec(TAB2)。setIndicator(表2)),
            FragmentB.class,ARG2);
        返回tabHost;    }

I am following this turtorial http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

here in Homefragment i set the fragmenttabhost,the issue is the view of tab is not displaying

public class HomeFragment extends Fragment {


    private FragmentTabHost mTabHost;

    public HomeFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.my_parent_fragment, container, false);

        mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
        mTabHost.setup(getActivity(), getFragmentManager(), android.R.id.tabcontent);

        mTabHost.addTab(
                mTabHost.newTabSpec("tab1").setIndicator("DISCOVER", null),
                FragmentA.class, null);
        mTabHost.addTab(
                mTabHost.newTabSpec("tab2").setIndicator("SHOP", null),
                FragmentB.class, null);

        return rootView;
    }


}

FragmentA.java

public class FragmentA extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fraga, container, false);
        Toast.makeText(getActivity(), "DISCOVER", Toast.LENGTH_LONG).show();
        return rootView;
    }

}

fraga.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0000"
    >

    <TextView
        android:id="@+id/txtLabel"
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="16dp"
        android:text="DISCOVER"/>



</RelativeLayout>

解决方案

Try this..

public class HomeFragment extends Fragment {

    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;

    private FragmentTabHost tabHost;

    public HomeFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        tabHost = new FragmentTabHost(getActivity());
        tabHost.setup(getActivity(), getChildFragmentManager(), R.layout.my_parent_fragment);

        Bundle arg1 = new Bundle();
        arg1.putInt("Arg for Frag1", 1);
        tabHost.addTab(tabHost.newTabSpec("Tab1").setIndicator("Tab1")),
                FragmentA.class, arg1);

        Bundle arg2 = new Bundle();
        arg2.putInt("Arg for Frag2", 2);
        tabHost.addTab(tabHost.newTabSpec("Tab2").setIndicator("Tab 2")),
            FragmentB.class, arg2);


        return tabHost;

    }

这篇关于片段tabhost不在片段工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆