Android : How do I make Tabwidget inside fragment

on Tuesday, August 12, 2014


Sorry for my poor English skill.


My prog make navigation drawer for 3 fragment. I want to make fragment_1 with tab widget + 3 tab sub fragment.


This is my MainActivity



public class MainActivity extends FragmentActivity implements NavigationDrawerFragment.NavigationDrawerCallbacks {

private NavigationDrawerFragment mNavigationDrawerFragment;
private CharSequence mTitle;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager()
.findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();

// Set up the drawer.
mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}


and Activity_main



<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.seoulin.MainActivity" >

<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<fragment
android:id="@+id/navigation_drawer"
android:name="com.example.seoulin.NavigationDrawerFragment"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>


I want to make tab inside homefragment(fragment_1) this is fragment_1 but it's not working :(. plz help or give advise for me. Thanks.



public class Fragment_Tab_1 extends Fragment{

private FragmentTabHost mTabHost;

//Mandatory Constructor
public Fragment_Tab_1() {
}

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

}

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

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

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

mTabHost.addTab(mTabHost.newTabSpec("fragmenta").setIndicator("Fragment A"), FragmentA.class, null);
mTabHost.addTab(mTabHost.newTabSpec("fragmentb").setIndicator("Fragment B"), FragmentB.class, null);
mTabHost.addTab(mTabHost.newTabSpec("fragmentc").setIndicator("Fragment C"), FragmentC.class, null);

return rootView;
}
}

0 comments:

Post a Comment