-1

I want to implement SlidingTabs with view pager. I followed the steps specified in this link. There is only one difference in my situation. I am using fragment instead of activity. So in this situation I want to inflate child fragments when user press the tabs.

Here how I tried to do this :

//I am using navigation drawer in my activity and when user press one item it create a fragment which I want to show many child fragments using tabs
@Override
public void onItemClick(int position) {

    switch (position){
        case 3 :
            mFragmentManager = getSupportFragmentManager();
            mFragment = new MainFragment().newInstance(mHelpLiveo.get(position).getName());

            if (mFragment != null){
                mFragmentManager.beginTransaction().replace(R.id.container, mFragment).commit();
            }
            break;
    }
}

my onCreateView function in my MainFragment.java :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.group_tabs, container, false);

    // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.

    //EDİT 3 :
    adapter =  new ViewPagerAdapter(this.getChildFragmentManager(),Titles,Numboftabs);

    // Assigning ViewPager View and setting the adapter
    pager = (ViewPager) rootView.findViewById(R.id.private_public_pager);
    pager.setAdapter(adapter);

    // Assiging the Sliding Tab Layout View
    tabs = (SlidingTabLayout) rootView.findViewById(R.id.tabs);
    tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width

    // Setting Custom Color for the Scroll bar indicator of the Tab View
    tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
        @Override
        public int getIndicatorColor(int position) {
            return getResources().getColor(R.color.blue_grey_900);
        }
    });

    // Setting the ViewPager For the SlidingTabsLayout
    tabs.setViewPager(pager);
    return rootView;
}

in my ViewPagerAdapter class :

//This method return the fragment for the every position in the View Pager
@Override
public Fragment getItem(int position) {

    if(position == 0) // if the position is 0 we are returning the First tab
    {
        Tab1 tab1 = new Tab1();
        return tab1;
    }
    else             // As we are having 2 tabs if the position is now 0 it must be 1 so we are returning second tab
    {
        Tab2 tab2 = new Tab2();
        return tab2;
    }

}

anything else is same with the link I provided above.

Edit 1 :

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.melomg.example.ui.MainFragment">

<com.melomg.example.ui.SlidingTabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="2dp"
    android:background="@color/myColor"/>

<android.support.v4.view.ViewPager
    android:id="@+id/private_public_pager"

    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_weight="1"
    ></android.support.v4.view.ViewPager>
</LinearLayout>

in mainfragment.xml file only SlidingTabLayout appears I am not able to see Vİew Pager.

Edit 2 :

some screens to explain my application.

my main activity with navigation drawer. when user press one item my main fragment is created my main activity with navigation drawer. when user press one item my main fragment is created

enter image description here my main fragment to show tabs. but view pager can not be seen.

melomg
  • 699
  • 3
  • 18
  • 35
  • 2
    instead of getFragmentManager() use getChildFragmentManager() – H Raval Dec 02 '15 at 09:57
  • I was using this.getActivity().getSupportFragmentManager() in my main fragment. how can I call getChildFragmentManager ? – melomg Dec 02 '15 at 10:00
  • adapter = new ViewPagerAdapter(this.getChildFragmentManager(),Titles,Numboftabs); I did like this. But still I am not able to see my tab fragments. – melomg Dec 02 '15 at 10:14
  • Use `TabLayout` with `ViewPager`, it'll help you to improve flow of projects. Even you can use multiple nested Fragments. – Anshul Tyagi Dec 02 '15 at 11:03

1 Answers1

1

In your mainfragment.xml layout, the LinearLayout orientation is set to horizontal, so the items are on the same line horizontally. The SlidingTabLayout's width is match_parent so the ViewPager is squished to it's right with width 0. Simply change the orientation to 'vertical'

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.melomg.example.ui.MainFragment">

...

</LinearLayout>