Lesson 2 Add a title for ViewPage

Source: Internet
Author: User

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 ));}}}
      
     
    
   


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.