4

I viewed the following question

FragmentTabHost with horizontal scroll and still was unable to get scrollable horizontal tabs in android. All the tabs stick to each other.

The onCreateView method of my fragment

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

    View rootView = inflater.inflate(R.layout.fragment_home, container, false);
    tabHost = (FragmentTabHost) rootView.findViewById(R.id.tabhost);

    tabHost.setup(getActivity(),getFragmentManager()/* getChildFragmentManager()*/, R.layout.fragment_home);

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

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

    Bundle arg3 = new Bundle();
    arg3.putInt("Arg for Frag2", 3);
    tabHost.addTab(tabHost.newTabSpec("Tab3").setIndicator("My Videos"), HomeFragmentChild2.class, arg3);

    Bundle arg4 = new Bundle();
    arg4.putInt("Arg for Frag2", 4);
    tabHost.addTab(tabHost.newTabSpec("Tab4").setIndicator("Videos"), HomeFragmentChild2.class, arg4);

    Bundle arg5 = new Bundle();
    arg5.putInt("Arg for Frag2", 5);
    tabHost.addTab(tabHost.newTabSpec("Tab5").setIndicator("Sample Videos"), HomeFragmentChild2.class, arg5);

    TabWidget tw = (TabWidget) rootView.findViewById(R.id.tabs);
    LinearLayout ll = (LinearLayout) tw.getParent();
    HorizontalScrollView hs = new HorizontalScrollView(this.getActivity());
    hs.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT));
    ll.addView(hs, 0);
    ll.removeView(tw);
    hs.addView(tw);
    hs.setHorizontalScrollBarEnabled(false);
    return tabHost;
}

XML :

<android.support.v13.app.FragmentTabHost                                                                               xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+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="@+id/tabs"
        android:layout_width="match_parent"a
        android:layout_height="wrap_content"
        android:orientation="horizontal" />

    <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="match_parent"
        android:layout_weight="1" />
   </LinearLayout>

 </android.support.v13.app.FragmentTabHost>

Additionally, would it be possible to achieve a way to swipe and change the tab ? Possible errors at my end?? Would be of great help ! Thanks!

Community
  • 1
  • 1
gursahib.singh.sahni
  • 1,559
  • 3
  • 26
  • 51

1 Answers1

0

Using ViewPager would be a solution.

Please check this link: Using ViewPager for Screen Slide

Also see : Creating Swipe Views with Tabs

Kannan Suresh
  • 4,573
  • 3
  • 34
  • 59