API 21 Working with movable tabs
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 couldn't find a tutorial or sample. I'm trying Google's SlidingTabs sample, but it's not built for Eclipse, so I don't know how to integrate it into my application. Can anyone help me? Maybe send me a link to a basic tutorial? Thank!
source to share
If this ViewPager
is the one you are talking about and not the type NavigationDrawer
, it continues to perform well on API-21.
Take a look at the code of an IOSched Google Play display app like ViewPager.
EDIT
Yes setNavigationMode
deprecated as a method and as a UI / UX pattern.
From the official documentation :
This method was deprecated in API level 21.
Action bar navigation modes are deprecated and not supported by inline toolbar action bars.
Consider using other common navigation patterns instead.
What are the links to common navigation patterns .
EDIT2
Fixed link to iosched code.
source to share
Just import these 2 classes, SlidingTabStrip and SlidingTabLayout in your project to have tabs in your application. You can look at the sample project to see how it is used.
Very easy to use. First import these 2 classes in your project and in your xml layout (my_layout.xml), add SlidingTabLayout. I also added a viewPager if 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 to be added to the SlidingTabLayout. For example:
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;
}
}
source to share