Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

API 21 Sliding tabs activity

I want to have two tabs to navigate between Fragments, I did it with the old method but now with API 21 all methods are deprecated. I searched a lot but I didn't found any tutorial or sample. I try the SlidingTabs sample from Google but it's not built for Eclipse so I don't know how to integrate it in my app. Can someone help me? Maybe send me a link to a basic tutorial? Thank you!

like image 572
MattButtMatt Avatar asked Dec 07 '25 22:12

MattButtMatt


2 Answers

Check this out, it may help you , it's a very well done menu for navigate through fragments

like image 59
Giacomo De Bacco Avatar answered Dec 09 '25 14:12

Giacomo De Bacco


Just import those 2 classes : SlidingTabStrip and SlidingTabLayout in your project to have tabs in your application. You can take a look at the sample project to see how it is used.

Very simple to use. First import those 2 classes in your project and in your xml layout(my_layout.xml), add the SlidingTabLayout. I also added a viewPager in case you want to use it with a ViewPager :

<LinearLayout 
    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"      
    android:orientation="vertical"
    android:paddingRight="@dimen/activity_horizontal_margin"
    tools:context="MyActivity">

      <SlidingTabLayout
            android:id="@+id/sliding_tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

     <android.support.v4.view.ViewPager
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/my_pager" />  

</LinearLayout>

In your activity,

public class MyActivity extends ActionBarActivity {
      private SlidingTabLayout slidingTabLayout;
      private ViewPager viewPager;

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

         viewPager = (ViewPager) findViewById(R.id.my_pager);
         viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));

         slidingTabLayout = (SlidingTabLayout)findViewById(R.id.sliding_tabs);
         slidingTabLayout.setViewPager(viewPager);
     }
}

And then you can define your adapter that will added to the SlidingTabLayout. For instance :

public static class MyAdapter extends FragmentPagerAdapter {
    private static final int FRAGMENT_1 = 0;
    private static final int FRAGMENT_2 = 1;
    private static final int FRAGMENT_3 = 2;

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

    @Override
    public Fragment getItem(int i) {
        switch (i){
            case FRAGMENT_1 : return new Fragment1();
            case FRAGMENT_2 : return new Fragment2();
            case FRAGMENT_3 : return new Fragment3();
        }
        return null;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position){
            case FRAGMENT_1 : return "Fragment 1 Title";
            case FRAGMENT_2 : return "Fragment 2 title";
            case FRAGMENT_3 : return "Fragment 3 Title";
        }
        return super.getPageTitle(position);
    }

    @Override
    public int getCount() {
        return 3;
    }
}
like image 38
Dimitri Avatar answered Dec 09 '25 13:12

Dimitri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!