0

I want to develop a top sliding menu as the following.

Tab example

I have seen many tutorials, but everyone works with fragments that makes the same function. What should I use to work with fragments with their own function? And, can I also separate the fragments' code in various files? (1 file per fragment I mean).

I've understood that working with Activities in this context is deprecated (TabActivity)

This class was deprecated in API level 13.
New applications should use Fragments instead of this class; to continue to
run on older devices, you can use the v4 support library which provides a
version of the Fragment API that is compatible down to DONUT.
Santiago Gil
  • 1,152
  • 4
  • 17
  • 47
  • are you want to use different-different fragments with these tabs? – Chirag Arora Aug 17 '16 at 08:53
  • 1
    @SantiGil try this demo: http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/ you can create different fragment and change in xml file according to your requirement – Damini Mehra Aug 17 '16 at 08:54
  • @ChiragArora Yes, I want for example a profile tab, another tab which lists products (different functions I mean). And also, a fragment will create another fragment, maintaining the tab menu (like when you start a new activity I mean). – Santiago Gil Aug 17 '16 at 08:57
  • ok please check my below answer if you want to know more than please reply.. – Chirag Arora Aug 17 '16 at 09:01
  • Thanks both, it is exactly what I searched for. – Santiago Gil Aug 17 '16 at 09:08

1 Answers1

3

Use Toolbar with View Pager like below :

Xml File :

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"  />

Class file code like :

public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;

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

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFrag(new OneFragment(), "ONE");
    adapter.addFrag(new TwoFragment(), "TWO");
    adapter.addFrag(new ThreeFragment(), "THREE");
    adapter.addFrag(new FourFragment(), "FOUR");
    adapter.addFrag(new FiveFragment(), "FIVE");
    adapter.addFrag(new SixFragment(), "SIX");
    adapter.addFrag(new SevenFragment(), "SEVEN");
    adapter.addFrag(new EightFragment(), "EIGHT");
    adapter.addFrag(new NineFragment(), "NINE");
    adapter.addFrag(new TenFragment(), "TEN");
    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFrag(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

}

For more detail please follow below link : Tabs with pager

Chirag Arora
  • 816
  • 8
  • 20
  • If I want that a fragment "calls" another fragment, and I want to maintain the tab menu (and also the current tab), is this possible? – Santiago Gil Aug 17 '16 at 09:10
  • 1
    yes @SantiGil please check the below link for your this query http://stackoverflow.com/questions/7723964/replace-fragment-inside-a-viewpager – Chirag Arora Aug 17 '16 at 09:19