1. Add the android support package
Because several classes are provided in the android support package, we first add the android-support-v4.jar file to the libs of the project Just go to the directory.
2. activity_main.xml
android:layout_height="match_parent"
android:orientation="vertical" > ;
xml uses PagerTabStrip as a sub-tag of ViewPager and cannot be taken out, otherwise an error will still be reported
3. In the PagerTabStrip tag, you can use the attribute android:layout_gravity=TOP|BOTTOM to specify the position of the title
4. If To display the title of a page in PagerTabStrip, you need to implement getPageTitle(int)
in the ViewPager adapter -->
use using id ‐ ‐ ‐ ‐ ‐ ‐ ‐ android: t ;
android:layout_height="wrap_content "
android: -
android:layout_gravity = "Bottom"
Android: Visibility = "Gone"/& GT; elayout & gt ;
There are two controls, PagerTitleStrip and PagerTabStrip, which must be used as sub-controls of ViewPager, otherwise an error will be reported.
What is the difference between the two controls?
The simple understanding is:
PagerTabStrip: interactive
PagerTitleStrip: non-interactive
After a simple test, it was found:
PagerTabStrip :
① Click the title above to switch ViewPager.
③ Display full-width underline (setDrawFullUnderline)
PagerTitleStrip:
① Clicking on the title above does not respond.
② No above description.
3. Set the title value
PagerAdater has a getPageTitle, which needs to be rewritten, and then different titles can be returned according to different pages
private class MyPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return mImageListView.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(mImageListView.get(position));
return mImageListView.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(mImageListView.get(position));
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "页面0";
case 1:
return "页面1";
case 2:
return "页面2";
case 3:
return "页面3";
}
return super.getPageTitle(position);
}
}
效果如下: