I followed Vivz’s example on youtube, but when the method became outdated, I had to find another way. Instead of adding tabs to the action bar, try:
Change adapter:
public class CollectionPagerAdapter extends FragmentStatePagerAdapter { private String[] titles = {"Item 1", "Item 2", "Item 3" }; public CollectionPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int i) { switch(i){ case 0: return new FragmentA(); case 1: return new FragmentB(); case 2: return new FragmentC(); } return null; } @Override public int getCount() { return titles.length; } @Override public CharSequence getPageTitle(int position) { return titles[position]; } }
And in the exercise that you would like to implement on the tabs, try
public class Tabtest extends ActionBarActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tab_test); ViewPager pager = (ViewPager) findViewById(R.id.your_view_pager); pager.setAdapter(new CollectionPagerAdapter(getSupportFragmentManager())); PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs); tabs.setViewPager(pager); }
Now, if you want to tag your tabs, such as the Google Play store, with a moving indicator called tabs and navigate while the user scrolls through this library
compile 'com.astuetz:pagerslidingtabstrip:1.0.1'
And modify the viewpager layout file as follows:
<LinearLayout //obviously add width and height and other necessery stuff android:orientation="vertical"> <com.astuetz.PagerSlidingTabStrip android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="48dip" /> <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" /> </LinearLayout>
And then you get the desired effect.
Hope this helps !!!
user4346621
source share