片段 tabhost 在片段中不起作用
Fragment tabhost is not working in Fragment
我正在关注这个教程http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/
我在 Homefragment 中设置了 fragmenttabhost,问题是选项卡的视图不显示
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>
试试这个..
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;
}
我正在关注这个教程http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/
我在 Homefragment 中设置了 fragmenttabhost,问题是选项卡的视图不显示
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>
试试这个..
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;
}