ViewPage Lesson 1

Source: Internet
Author: User

ViewPage Lesson 1
Step 1:
Add the following content to the layout file: Because android. support. v4.view is an extension package, this format is also used when you use a semantic view.


Android: id = "@ + id/vp_viewpage"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">



Step 2:

Create four layout files: page01.xml, page02, page03, page04

 
     
      
  
 




Step 3:

MainActivity:

Public class MainActivity extends Activity {private ViewPager viewpager; private List
 
  
Children; @ 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); viewpager. setAdapter (new InnerPagerAdapter ();} public class InnerPagerAdapter extends PagerAdapter {@ 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.