With the help of this tutorial I have created a PagerAdapter for my Android App.
Now I like to add a title to each page (like in the Play Store).
I found this library: http://viewpagerindicator.com/
But I can’t find out how to implement it in my app or how to use it.
Can someone please explain the usage to me?
Here’s the code of my PagerAdapter, if it is needed:
public class MyPagerAdapter extends PagerAdapter {
public int getCount() {
return 5;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch(position) {
case 0:
resId = R.layout.farleft;
break;
case 1:
resId = R.layout.left;
break;
case 2:
resId = R.layout.middle;
break;
case 3:
resId = R.layout.right;
break;
case 4:
resId = R.layout.farright;
break;
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}
PagerTabStripandPagerTitleStripare in the Android Support package, where you gotViewPagerfrom. Simply add one of those as a child of yourViewPager, withandroid:layout_gravityindicating whether you want the strip on the top or bottom. Then, as Luksprog indicated, addgetPageTitle()to yourPagerAdapter.Here is a sample project demonstrating the use of
PagerTabStrip.Basic instructions are on the page that you linked to. Add one of the ViewPagerIndicator widgets to your layout, positioned where you want it (e.g., use a
verticalLinearLayoutto stack one above yourViewPager). Sometime after you callsetAdapter()on theViewPager, callsetViewPager()on the indicator widget, supplying it yourViewPager. Once again, you will also need to implementgetPageTitle()on yourPagerAdapter.