ViewPager is used to some extent in many applications. Its use provides us with great convenience for APP development, which can reduce the creation of our activities and facilitate the switching between pages. This blog briefly introduces ViewPager and PagerTabStrip combined use. First we need to add the ViewPager control to the XML layout and place the PagerTabStrip control in the ViewPager, for example:
<android.support.v4.view.ViewPager android:id="@+id/BasicI_vp" android:layout_width="wrap_content" android:layout_height="wrap_content"> <android.support.v4.view.PagerTabStrip android:id="@+id/VPtitle_pts" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_gravity="top"> </android.support.v4.view.PagerTabStrip> </android.support.v4.view.ViewPager>
Then define their respective objects in the activity to process them, add a custom PagerAdapter object to the ViewPager object, and add the getPageTitle() method to the custom PagerAdapter class. The code is as follows:
viewPager = (ViewPager) findViewById(R.id.BasicI_vp); pagerTabStrip = (PagerTabStrip) findViewById(R.id.VPtitle_pts); //Set the underline not to be displayed pagerTabStrip.setDrawFullUnderline(false); //set background color pagerTabStrip.setBackgroundColor(Color.BLUE); //Set the selected background color pagerTabStrip.setTabIndicatorColor(Color.RED); informationViews = new ArrayList<View>(); initData (); viewPager.setAdapter(new MyViewPagerAdapter());
@Override public CharSequence getPageTitle(int position) { System.out.println(title[position]); return title[position]; }
After this setting, if it does not display or report an error, then add the following code to the activity:
((ViewPager.LayoutParams) pagerTabStrip.getLayoutParams()).isDecor = true;