with - tabs in android studio



Sliding tabs inside actionbar tabs (1)

It seems already managed by Android: you just need to create a ViewPager in the outer activity and for every fragment hosted by the ViewPager, you create a ViewPager. This is my test project:

MainActivity.java

public class MainActivity extends AppCompatActivity {

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

        MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager());
        ViewPager viewPager = (ViewPager) findViewById(R.id.pager_activity);
        viewPager.setAdapter(adapter);
    }

    private class MyPagerAdapter extends FragmentStatePagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public android.support.v4.app.Fragment getItem(int i) {
            return new InnerFragment();
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return "ACTIVITY TITLE " + (position+1);
        }
    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager_activity"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v4.view.PagerTitleStrip
            android:id="@+id/pager_title_strip_activity"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:background="#33b5e5"
            android:textColor="#fff"
            android:paddingTop="4dp"
            android:paddingBottom="4dp" />

    </android.support.v4.view.ViewPager>

</RelativeLayout>

InnerFragment.java (one of the fragment hosted inside the activity viewpager)

public class InnerFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_inner, container, false);
        ViewPager pager = (ViewPager) root.findViewById(R.id.pager);
        MyPagerAdapter adapter = new MyPagerAdapter(getChildFragmentManager());
        pager.setAdapter(adapter);
        return root;
    }

    private class MyPagerAdapter extends FragmentStatePagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public android.support.v4.app.Fragment getItem(int i) {
            return PageFragment.newInstance(i+1);
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return "FRAGMENT " + (position+1) + " TITLE " + (position+1);
        }
    }
}

and its layout fragment_inner.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="it.test.testflipboardviewpager.InnerFragment">

    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v4.view.PagerTitleStrip
            android:id="@+id/pager_title_strip"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:background="#33b5e5"
            android:textColor="#fff"
            android:paddingTop="4dp"
            android:paddingBottom="4dp" />

    </android.support.v4.view.ViewPager>

</FrameLayout>

PageFragment.java - one of the fragment hosted by the InnerFragment viewpager

public class PageFragment extends Fragment {

    private int mPosition;

    public static PageFragment newInstance(int position) {
        PageFragment fragment = new PageFragment();
        Bundle args = new Bundle();
        args.putInt("position", position);
        fragment.setArguments(args);
        return fragment;
    }

    public PageFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mPosition = getArguments().getInt("position");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View root = inflater.inflate(R.layout.fragment_page, container, false);
        TextView textView = (TextView) root.findViewById(R.id.text);
        textView.setText("" + mPosition);
        return root;
    }
}

and its layout fragment_page.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="it.test.testflipboardviewpager.PageFragment">

    <TextView
        android:id="@+id/text"
        android:textSize="25sp"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

that's it! Try and let me know.

Edit: Forgot to say that the first component is not an ActionBar.

I am trying to implement sliding tabs inside actionBar tabs just like flipboard. So one of the main tabs will have three tabs. Sub-tabs will scroll first and once all tabs get scrolled, then main tab comes into focus and will start scrolling.

I am attaching screenshot of flipBoard UI

Have anyone done this? Please help.





android-actionbar-tabs