Viewpager Basic Mode Load view

Source: Internet
Author: User

1. New View_pager.xml

<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"    >    <Android.support.v4.view.ViewPagerAndroid:id= "@+id/viewpager"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center" /></Relativelayout>

2. Create a new Viewpageradapter class to load the adapter for an instance of the Viewpager layout;

 Public class extends pageradapter{    Private  context context;     Private List<view> viewlist;      Public Viewpageradapter (list<view> views,context context) {        this. Context= context;         this. views = views ;    }
4 Ways to re-
//getcount () returns the size of the list<view>:    @Override    publicint  GetCount () {         // TODO auto-generated Method stub        return views.size ();    }
//instantiateitem (): Adds the current view to ViewGroup and returns the current view    @Override    publicint  position) {        Container.addview (views.get (position));         return Views.get (position);    } Destroyitem (): Delete the current view;  
  @Override    publicvoidint  position, Object object) {        Container.removeview (Views.get (position));    } Isviewfromobject determines whether the current view is the same as the object (value of view) we want, and returns Trun/false;
    @Override    publicboolean  isviewfromobject (View arg0, Object arg1) {         // TODO auto-generated Method stub        return (arg0 = = arg1);}    }

3. Create the Guide class, get the View object, place the View object in the list<view>, then use list<view> in the adapter viewpageradapter, and finally get the Viewpager and

 Public classGuideextendsactivity{PrivateList<view>ListView; PrivateViewpager Viewpager; PrivateViewpageradapter Vpadapter; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);            Requestwindowfeature (Window.feature_no_title);            Setcontentview (R.layout.view_pager);        Init (); }        Private voidinit () {Layoutinflater inflater= Layoutinflater.from ( This); ListView=NewArraylist<view>(); //Get View ObjectView view1 = inflater.inflate (R.LAYOUT.VIEW_PAGER_IMG1,NULL); View View2= Inflater.inflate (R.layout.view_pager_img2,NULL); View VIEW3= Inflater.inflate (R.LAYOUT.VIEW_PAGER_IMG3,NULL); View View4= Inflater.inflate (R.LAYOUT.VIEW_PAGER_IMG4,NULL); //The View object is placed in the list<view>Listview.add (View1);            Listview.add (VIEW2);            Listview.add (VIEW3);            Listview.add (VIEW4); //List<view> placed in the adapter ViewpageradapterVpadapter =NewViewpageradapter (ListView, This); //get ViewPage, set adapter;Viewpager =(Viewpager) Findviewbyid (R.id.viewpager);        Viewpager.setadapter (Vpadapter); }    }

4. Create view_pager_img.xml;

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"    ><ImageViewAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:background= "@drawable/guide_background2"/></LinearLayout>

Viewpager Basic Mode Load view

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.