Lesson 2 Add a title for ViewPage
In Lesson 1
Prerequisites: Understanding ViewPage Lesson 1 http://blog.csdn.net/wei_chong_chong/article/details/50468832
Add a title for ViewPage:
PageTabStrip is a subclass of PageTitleStrip.
The use of PageTabStrip is exactly the same as that of PageTitleStrip,
The title text of pageTabStrip carries an Indicator, and you can click the title to switch to the display page.
Directly add
Layout file:
In
The details are as follows:
Step 2: add
Private List Titles;
And initialize
Titles = new ArrayList ();
Titles. add ("page01 ");
Titles. add ("page02 ");
Titles. add ("page03 ");
Titles. add ("page04 ");
And implementation methods in the internal class:
@ Override
Public CharSequence getPageTitle (int position ){
// TODO Auto-generated method stub
Return titles. get (position );
}
The details are as follows:
Public class MainActivity extends Activity {private ViewPager viewpager; private List
Children; private List
Titles; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); viewpager = (ViewPager) findViewById (R. id. vp_viewpage); children = new ArrayList
(); Children. add (LayoutInflater. from (this ). inflate (R. layout. page01, null); children. add (getLayoutInflater (). inflate (R. layout. page02, null); children. add (getLayoutInflater (). inflate (R. layout. page03, null); children. add (getLayoutInflater (). inflate (R. layout. page04, null); titles = new ArrayList
(); Titles. add ("page01"); titles. add ("page02"); titles. add ("page03"); titles. add ("page04"); viewpager. setAdapter (new InnerPagerAdapter ();} public class InnerPagerAdapter extends PagerAdapter {@ Overridepublic CharSequence getPageTitle (int position) {// TODO Auto-generated method stubreturn titles. get (position) ;}@ Overridepublic int getCount () {// get the number of child-level la s return children. size (); // returns the number of neutron-level la s in the viewpage container.} @ Overridepublic boolean isViewFromObject (View view, Object object) {// determine whether a View object is the object Currently added to the ViewPager container. return view = object ;} // The following two methods are the non-abstract methods that must be implemented in PageAdapter @ Overridepublic Object instantiateItem (ViewGroup container, int position) {// instantiate the View object that needs to be displayed at the position specified in the ViewPager container. View = children. get (position); container. addView (view); return view ;}@ Overridepublic void destroyItem (ViewGroup container, int position, Object object) {// remove the View Object container at the specified position from ViewPager. removeView (children. get (position ));}}}