Android Growth Diary-use Pageradapter for page switching

Source: Internet
Author: User

TIP: This approach allows for page switching

1. Create View1.xml,view2.xml,view3.xml,main.xml

Create in Main.xml

<android.support.v4.view.viewpager

android:id="@+id/pager"

Android:layout_width="Wrap_content"

android:layout_height="Wrap_content">

</android.support.v4.view.ViewPager>

2. Create Mainactivity.java

Public class Mainactivity extends Activity {

Private list<view>viewlist; [S1]

Private Viewpager Pager;

@Override

protected void onCreate (Bundle savedinstancestate) {

Super. OnCreate (savedinstancestate);

Setcontentview (r.layout. Activity_main);

viewlist=New arraylist<view> ();

Pager= (Viewpager) Findviewbyid (r.id. Pager); [S2]

Convert layouts to Views

View View1=view. Inflate (this, r.layout. view1,null);

View View2=view. Inflate (this, r.layout. view2, null);

View View3=view. Inflate (this, r.layout. view3, null); [S3]

Viewlist.add (View1);

Viewlist.add (VIEW2);

Viewlist.add (VIEW3); [S4]

Creating the Pageradapter Adapter

Myviewpageradapter adapter=New myviewpageradapter (viewlist);

Viewpager Load Adapter

Pager.setadapter (adapter);

}

}

3. Create an adapter Myviewpageradapter.java

Public class Myviewpageradapter extends pageradapter {

Private list<view>viewlist; [S5]

Public Myviewpageradapter (list<view> viewlist)

{

this. viewlist=viewlist;

}[S6]

@Override

Public int GetCount () {

Returns the number of page cards

return viewlist.size ();

}

@Override

Public boolean isviewfromobject (View arg0, Object arg1) {

Whether the view is from an object

return arg0==arg1;

}

@Override

Public Object Instantiateitem (ViewGroup container, int position) {[S7]

Instantiate a single page card

Container.addview (Viewlist.get (position));

return viewlist.get (position);

}

@Override

Public void Destroyitem (ViewGroup container, int position, object object) {[S8]

Destroy a page card

Container.removeview (Viewlist.get (position));

}[S9]

[S1] Create collection list, generic to view

[S2] Control instantiation

[S3] converting layouts to views

[S4] Add to Collection

[S5] Declares a list collection

[S6] Creates a constructor method for receiving a collection that has been transmitted

[S7] instantiating a page card

[S8] destroying page cards

[S9] Two new methods

Android Growth Diary-use Pageradapter for page switching

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.